Place the NSCollectionLayoutSupplementaryItem at the bottom of the containing group

Viewed 188

The context:

I'm creating cells using the UICollectionViewCompositionalLayout and an estimated height, based on dynamic text within the cells. Each cell has a supplementary item (a button) in the bottom trailing corner. A small excerpt of the code which creates the layout:

// List item
let estimatedListItemHeight = CGFloat(240)

let listItemSize = NSCollectionLayoutSize(
    widthDimension: .fractionalWidth(1/3),
    heightDimension: .estimated(estimatedListItemHeight)
)

let listItem = NSCollectionLayoutItem(layoutSize: listItemSize, supplementaryItems: [makeOverflowButton()])

// List item group
let listItemGroupSize = NSCollectionLayoutSize(
    widthDimension: .fractionalWidth(1),
    heightDimension: .estimated(estimatedListItemHeight)
)

let listItemGroup = NSCollectionLayoutGroup.horizontal(
    layoutSize: NSCollectionLayoutSize(
        widthDimension: .fractionalWidth(1),
        heightDimension: .estimated(estimatedListItemHeight)
    ),
    subitem: listItem,
    count: 3
)

Then I use a separate method to create the supplementary item:

func makeOverflowButton() -> NSCollectionLayoutSupplementaryItem {

    let overflowSize = NSCollectionLayoutSize(
        widthDimension: .fractionalWidth(1),
        heightDimension: .estimated(25)
    )

    return NSCollectionLayoutSupplementaryItem(
        layoutSize: overflowSize,
        elementKind: OverflowSupplementaryButton.supplementaryViewKind,
        containerAnchor: NSCollectionLayoutAnchor(edges: [.bottom, .trailing])
    )
}

The issue:

Because the cells are dynamic in height, the supplementary items are not on the same y position with one another. I do however want them to be dynamic as well as having each supplementary item on the same y. See the image below:

requested result

An idea could maybe be to instead be able to put supplementary items on their containing group (listItemGroup in my excerpt from above), but is there a way to put supplementary items on a NSCollectionLayoutGroup?

If not, what would be the best way to achieve this result?

0 Answers
Related