0

Im trying to use a compositional layout to create random count of group like here

enter image description here

In other words I need to have a first group with 10 elements, second - 5 elements, third - 8 elements, for example. All elements are calculated in another business logic, they can change.

Where: Blue - header

Red - section

Green - group

black - cell

How can I manage this ? I have played with nested groups but it seems that not suitable for me? It would be better if I go away from compos layout and try another ideas?

Example code which I try to use, but it seems that we can not set "estimated" count for group?

let layout = UICollectionViewCompositionalLayout {
    (_: Int, _: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in

    let trailingItem = NSCollectionLayoutItem(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .estimated(0)
        )
    )
    trailingItem.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)

    let trailingGroup = NSCollectionLayoutGroup.vertical(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(0.1),
            heightDimension: .fractionalHeight(1)
        ),
        subitem: trailingItem, count: 10
    )

    let nestedGroup = NSCollectionLayoutGroup.horizontal(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .fractionalHeight(1)
        ),
        subitems: [trailingGroup]
    )

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

    return section
}
NyanNyan
  • 51
  • 1
  • 9
  • You might need to make your green boxes the sections so that they can contain varying numbers of items. Or maybe you could have an invisible, non-selectable cell type to use as spacers when you have fewer than 10 items? – Geoff Hackworth Mar 28 '23 at 15:20
  • It seems that if I change scroll direction for horizontal in collection view - All cells will be scrolling horizontally. It would be easy ofk, Set horizontal scroll and vertical sections, but apple do not provide that. Empty cells not suitable, Im thinking about stay away from that idea and try to use scroll view with view pager or something else – NyanNyan Mar 28 '23 at 15:29

0 Answers0