0

I'm trying to replicate the Stickers app inside iMessage using the new UICollectionViewCompositionalLayout.

However, I can't seem to be able to reproduce the horizontal "paging" behavior.

There should be n + 1 sections, where n i the number of pages, to be aligned horizontally.

Desired behavior:

enter image description here

What I've come up with:

enter image description here

My Code:

@available(iOS 13.0, *)
class TestViewController: UIViewController, UICollectionViewDataSource {

    @IBOutlet weak var collection: UICollectionView!


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

        cell.backgroundColor = UIColor(hue: CGFloat(drand48()), saturation: 1, brightness: 1, alpha: 1)

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        collection.dataSource = self
        collection.collectionViewLayout = createLayout()
    }


    func createLayout() -> UICollectionViewLayout {
        let layout = UICollectionViewCompositionalLayout { (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in

            if sectionIndex == 0 {

                let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(49), heightDimension: .absolute(49))
                let item = NSCollectionLayoutItem(layoutSize: itemSize)

                let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(49), heightDimension: .absolute(49))
                let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

                let section = NSCollectionLayoutSection(group: group)
                section.orthogonalScrollingBehavior = .continuous
                section.contentInsets.leading = 16
                section.contentInsets.trailing = 16

                section.interGroupSpacing = 8

                return section
            } else {
                let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.2), heightDimension: .fractionalHeight(1.0))
                let item = NSCollectionLayoutItem(layoutSize: itemSize)

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

                let section = NSCollectionLayoutSection(group: group)

                return section
            }
        }
        return layout
    }
}
guidev
  • 2,695
  • 2
  • 23
  • 44
  • I haven't worked with `UICollectionViewCompositionalLayout` yet, so this is just a guess, but... I see you're not setting the bottom section's `orthogonalScrollingBehavior` to anything, which by default is `.none` meaning "The section does not allow users to scroll its content orthogonally." Have you tried setting it to something, like `.paging` or `.groupPaging`? – TylerP May 13 '20 at 00:41
  • @TylerTheCompiler yes, I've tried that. Unfortunately, if I enable `orthogonalScrollingBehavior` it behaves likes the first section. All the items are placed horizontally. – guidev May 13 '20 at 11:26

0 Answers0