0

For given layout, UICollectionView always returns content size which is equal to UICollectionView size, even if items go beyond the UICollectionView.

private lazy var layout: UICollectionViewCompositionalLayout = {
    let margin = 8.0
    let itemSize = NSCollectionLayoutSize(
        widthDimension: .fractionalWidth(1.0),
        heightDimension: .fractionalHeight(1.0)
    )
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    let groupSize = NSCollectionLayoutSize(
        widthDimension: .estimated(56),
        heightDimension: .fractionalHeight(1.0)
    )
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
    let section = NSCollectionLayoutSection(group: group)
    section.contentInsets = NSDirectionalEdgeInsets(top: .zero, leading: margin, bottom: .zero, trailing: margin)
    section.orthogonalScrollingBehavior = .continuous
    return UICollectionViewCompositionalLayout(section: section)
}()

Also I suspect, because of that I cannot programatically scroll to items beyond the screen. What can be wrong here?

I tried to change widthDimension of item and the group, contentInsets, but it didn't work.

UPDATE

The correct way to to this was to use:

NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 1)

and

section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary
dub.raf
  • 1
  • 2

2 Answers2

0

Try to change the size of the group.

Instead of this:

 let groupSize = NSCollectionLayoutSize(
    widthDimension: .estimated(56),
    heightDimension: .fractionalHeight(1.0)
)

Try:

let groupSize = NSCollectionLayoutSize(
    widthDimension: .estimated(56),
    heightDimension: .estimated(200)
)

I think the problem is that you didn't set the height for the group.

  • It didn't work also, issue still occurs. Moreover my UICollectionView has fixed height, so the group height is the same. – dub.raf Apr 12 '23 at 08:33
0

Try to use:

section.orthogonalScrollingBehavior = .paging

NexusUA
  • 217
  • 1
  • 3
  • 13