Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCompositionalLayout with groupPagingCentered doesn't start centered

My layout code is very simple, something that you will have seen in every tutorial or article about the new compositional layouts.

  func createLayout() -> UICollectionViewLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets = .init(top: 0, leading: 5, bottom: 0, trailing: 5)

    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .fractionalHeight(1.0))
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

    let section = NSCollectionLayoutSection(group: group)
    section.orthogonalScrollingBehavior = .groupPagingCentered

    let layout = UICollectionViewCompositionalLayout(section: section)
    return layout
  }

When I start the app, the cell is not properly centered. Only when I drag the cell by the tiniest amount, does it spring to its correct place.

Before:

enter image description here

After I drag it a tiny bit:

enter image description here

I have not seen any questions on SO about the same problem. Nobody on Twitter or in blogs talking about it. Not sure what I am doing wrong here?

like image 562
Kevin Renskers Avatar asked Jan 10 '20 16:01

Kevin Renskers


Video Answer


2 Answers

Maybe too late, but here is a workaround:

func createLayout() -> UICollectionViewLayout {
    let layout = UICollectionViewCompositionalLayout { (sectionIndex, environment) -> NSCollectionLayoutSection? in
        let sideInset: CGFloat = 5

        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        item.contentInsets = .init(top: 0, leading: sideInset, bottom: 0, trailing: sideInset)

        let groupWidth = environment.container.contentSize.width * 0.93
        let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(groupWidth), heightDimension: .fractionalHeight(1.0))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

        let section = NSCollectionLayoutSection(group: group)

        // add leading and trailing insets to the section so groups are aligned to the center
        let sectionSideInset = (environment.container.contentSize.width - groupWidth) / 2
        section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: sectionSideInset, bottom: 0, trailing: sectionSideInset)

        // note this is not .groupPagingCentered
        section.orthogonalScrollingBehavior = .groupPaging

        return section
    }

    return layout
}
like image 171
Kazune Haraguchi Avatar answered Oct 09 '22 14:10

Kazune Haraguchi


This is expected behavior. "centered" means a cell snaps to the center after scrolling. Before doing any scrolling, the whole group is scrolled all the way to the right. Your group is only 0.93 fractional width so the difference is slight. The effect is much less unsightly when the fractional width is smaller.

like image 23
matt Avatar answered Oct 09 '22 13:10

matt