1

我创建了一个自定义布局,基于UICollectionViewCompositionalLayout该布局在面板或卡片中显示每个部分的项目......

没有标题的面板

...这里的蓝色区域是NSCollectionLayoutDecorationItem通过其decorationItems属性传递给该部分的。这个背景项目有它自己的内容插图,它从集合视图边界(灰色区域)插入,并且该部分有它自己的内容插图,可以在面板中插入它。紫色部分是 3 个全宽项目(单元格)。

这一切都按预期工作。

但是,当我尝试使用 为每个部分添加标题时boundarySupplementaryItems,标题(红色)似乎忽略了该部分的内容插入,但该部分确实调整了自身的大小,就好像它定位正确......

带标题的面板

...标题将自身固定在部分的顶部,但部分高度会增加标题 + 插图的高度。

这是我用来生成布局的代码...

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")

谁能建议我如何让标题正确定位 - 就在该部分的第一项上方,并尊重已设置的内容插图?

4

0 回答 0