skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Try to use:

    section.orthogonalScrollingBehavior = .paging

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search