如何将NSCollectionLayoutBoundarySupplementaryItem与NSCollectionLayoutDecorationItem结合使用?

问题描述

我正在使用类似于iOS 14插入分组样式的UICollectionViewCompositionalLayout,其中节中的所有单元格都在“卡”内,而节头位于“卡”外。我已经可以正常工作了,但是NSCollectionLayoutBoundarySupplementaryItem似乎不能很好地与背景NSCollectionLayoutdecorationItem一起玩。

似乎NSCollectionLayoutBoundarySupplementaryItem对齐到节的顶部时位于装饰视图和插图内容所覆盖的区域之外。认情况下,布局通过标题的高度插入内容,但是标题放置在内容区域之外,因此如果不对标题的高度进行硬编码或尝试进行一些布局数学运算就无法对齐。正在努力避免。

以下是问题的屏幕截图:

enter image description here

这是我要重新创建的布局:

enter image description here

(忽略左插图-这部分很容易。)

您能够获得类似的功能吗?如果没有一些布局数学,是否有可能?

以下是相关代码

    // Create the layout item:
    let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),heightDimension: .estimated(64))
    let item = NSCollectionLayoutItem(layoutSize: size)

    // Create the layout group:
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: size,subitem: item,count: 1)

    // Create the layout header:
    let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),heightDimension: .estimated(48))
    let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
        layoutSize: headerSize,elementKind: UICollectionView.elementKindSectionHeader,alignment: .top)

    // Create the section background:
    let sectionBackground = NSCollectionLayoutdecorationItem.background(
        elementKind: UICollectionView.elementKindSectionBackground)
    sectionBackground.contentInsets = NSDirectionalEdgeInsets(all: Sizes.Insets.System.groupedSectionBackground)

    // Create the layout section:
    let section = NSCollectionLayoutSection(group: group)
    section.contentInsets = NSDirectionalEdgeInsets(all: Sizes.Insets.System.groupedSectionBackground)
    section.decorationItems = [sectionBackground]
    section.boundarySupplementaryItems = [sectionHeader]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)