9

我正在使用 UICollectionView 组合布局并希望有一个贴在顶部的标题

这应该是可能的

header.pinToVisibleBounds = true

但是,在滚动过程中,标题被奇怪地遮住了,看起来像一个错误......而且看起来只有在可见屏幕之外的单元格,当重新加载时,遮住了标题

迅速

您可以在此视频中看到它 https://www.dropbox.com/s/n5myzfceuiwzb39/stackoverflow-unsatable-header.mov?dl=0

这就是整个布局的构建方式

func buildLayout() -> UICollectionViewCompositionalLayout {

    // cells
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .absolute(90))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(90))
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
    group.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20)
    group.interItemSpacing = .fixed(10)

    let section = NSCollectionLayoutSection(group: group)
    section.interGroupSpacing = 10
    section.contentInsets = NSDirectionalEdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0)

    // header
    let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(200))
    let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
    header.pinToVisibleBounds = true
    header.zIndex = Int.max
    section.boundarySupplementaryItems = [header]

    let configuration = UICollectionViewCompositionalLayoutConfiguration()
    return UICollectionViewCompositionalLayout(section: section, configuration: configuration)
}

解决方法是什么?或者有什么其他方法可以有一个“浮动”标题?


注1:

看起来当标题附加到configuration.boundarySupplementaryItems它时,它的行为符合预期......通过这可能是当前情况的解决方案,它不能解决部分的整体问题......(您还必须将 zIndex 设置为非常高数字)

do {
    center.contentInsets.top = 0
    let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(200))
    let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
    header.pinToVisibleBounds = true
    header.zIndex = Int.max // !!! IMPORTANT
    //header.extendsBoundary = true
    configuration.boundarySupplementaryItems = [header]
}
4

0 回答 0