1 year ago

#383251

test-img

Tarek

UICollectionView orthogonalScrollingBehavior groupPagingCentered wrong behavior

So I have a collection view with the following layout

  private static func createChannelsCompositionalLayout() -> UICollectionViewLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(0.45))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets.top = 5
    item.contentInsets.bottom = 5
    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.87), heightDimension: .fractionalHeight(1))
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 2)
    group.contentInsets.trailing = 10
    group.contentInsets.leading = 10
    group.contentInsets.bottom = 10
    let section = NSCollectionLayoutSection(group: group)
    section.orthogonalScrollingBehavior = .groupPagingCentered
    let layout = UICollectionViewCompositionalLayout(section: section)
    return layout
  }

Basically it scrolls horizontally with two items displayed vertically per group. It works fine if the number of items is even; however if the number of items is odd, when you scroll to the last group (which would contain 1 item instead of 2), it wouldn't snap perfectly like it does with 2 items. It will be closer to the screen's edge and a bigger portion of the second to last group will be shown which is not desirable. Is that a bug or is there something I could change in my code?

I tried playing a little bit with the insets and fractions but no joy. I also tried adding a contentInset to the section but that didn't help either.

uicollectionview

uikit

0 Answers

Your Answer

Accepted video resources