I have created a custom layout based on UICollectionViewCompositionalLayout
that presents each section's items in a panel or card...
...here the blue area is a NSCollectionLayoutDecorationItem
that is passed to the section via its decorationItems
property. This background item has its own content insets that inset it from the collection view bounds (the grey area) and the section has its own content insets to inset it within the panel. The purple section is 3 full width items (cells).
This all works as expected.
However, when I try to add a header to each section using boundarySupplementaryItems
, the header (red) appears to ignore the section's content insets, but the section does size itself as though it's properly positioned...
...the header pins itself to the top of the section, but the section height increases by the height of the header + the insets.
Here is the code I'm using to generate the layout...
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44.0))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44.0))
let headerItem = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: "header", alignment: .top)
let panelBackgroundItem = NSCollectionLayoutDecorationItem.background(elementKind: "PanelBackground")
panelBackgroundItem.contentInsets = NSDirectionalEdgeInsets(top: 4.0, leading: 8.0, bottom: 4.0, trailing: 8.0)
let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [headerItem]
section.contentInsets = NSDirectionalEdgeInsets(top: 11.0, leading: 15.0, bottom: 11.0, trailing: 15.0)
section.decorationItems = [panelBackgroundItem]
let configuration = UICollectionViewCompositionalLayoutConfiguration()
configuration.interSectionSpacing = 4.0
let layout = UICollectionViewCompositionalLayout(section: section, configuration: configuration)
layout.register(PanelBackgroundView.self, forDecorationViewOfKind: "PanelBackground")
Can anyone advise on how I can get the header to position itself properly – just above the first item in the section, and respecting the content insets that have been set?