3

I'm having trouble with adding a footer in UICollectionViewCompositionalLayout on top of the cells. Specifically I want to have an interactive control in my footer (in my case a UIPageControl). This is an example layout,

 func generateLayout() -> UICollectionViewCompositionalLayout {
        let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
        let groupSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .absolute(213)
        )
        
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 1)
        
        let section = NSCollectionLayoutSection(group: group)
        section.boundarySupplementaryItems = [footer()]
        
        return UICollectionViewCompositionalLayout(section: section)
    }

The footer is configured like,

private func footer() -> NSCollectionLayoutBoundarySupplementaryItem {
        let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(20))
        let item = NSCollectionLayoutBoundarySupplementaryItem(
            layoutSize: size,
            elementKind: "footer-kind",
            alignment: .bottom,
            absoluteOffset: .init(x: 0, y: -30)
        )
        item.zIndex = 1000
        return item
    }

Lets say I have a button in my Footer that I want to be tappable. I have changed the zIndex of the footer so it is higher than other views in my section.

class FooterCell: UICollectionReusableView {
    override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
        super.apply(layoutAttributes)
        layer.zPosition = CGFloat(layoutAttributes.zIndex)
    }
}

The result of the snippets above is that the button is visible on top of the cells, but not tappable. After some inspections in the view debugger it is clear that the supplementary view is added to the view hierarchy before the cells and changing the zIndex only moves the layer to the front, not the complete view. Is there someway to change the order on how the views are added in the compositional layout? Or is there any other way to bring the footer view above the cells?

Simon
  • 55
  • 3

0 Answers0