0

我试图为我的部分提供不同的背景颜色/图像,但发现很难在 createCompositionalLayout() 中设置两个不同的背景视图。

下面是两个背景视图组件和部分。在布局创建方法中有各种注册类/笔尖的示例,但这只是为所有部分创建一个背景视图。

在 createCompositionalLayout() 中有一个用于返回 NSCollectionLayoutSection (部分)的开关,但要为每个部分返回已注册的背景视图布局是我的障碍。

   func createCompositionalLayout() -> UICollectionViewLayout {
        let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
            guard let sectionKind = SectionBack(rawValue: sectionIndex) else { return nil }
            let section = self.sections[sectionIndex]
            switch section.type {
            case "mediumTable":
                return self.createMediumTableSection(using: section)
            case "smallTable":
                return self.createSmallTableSection(using: section)
            default:
                return self.createFeaturedSection()
            }
        }
        return layout
    }

**Red backgroundview**
let backgroundItem = NSCollectionLayoutDecorationItem.background(elementKind: "background")
    let backgroundInset: CGFloat = 8
    backgroundItem.contentInsets = NSDirectionalEdgeInsets(top: backgroundInset, leading: 
backgroundInset, bottom: backgroundInset, trailing: backgroundInset)
    layoutSection.decorationItems = [backgroundItem]
    let layout = UICollectionViewCompositionalLayout(section: layoutSection)
    layout.register(RedBackgroundSupplementaryView.self, forDecorationViewOfKind:     
"background")

func createMediumTableSection(using section: Section) -> NSCollectionLayoutSection {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(0.33))

    let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
    layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5)

    let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .fractionalWidth(0.55))
    let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])

    let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
    layoutSection.orthogonalScrollingBehavior = .groupPagingCentered

    let layoutSectionHeader = createSectionHeader()
    layoutSection.boundarySupplementaryItems = [layoutSectionHeader]

    return layoutSection
}

func createSmallTableSection(using section: Section) -> NSCollectionLayoutSection {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(0.2))
    let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
    layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 0)

    let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .estimated(200))
    let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])

    let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
    let layoutSectionHeader = createSectionHeader()
    layoutSection.boundarySupplementaryItems = [layoutSectionHeader]

    return layoutSection
}
4

2 回答 2

0

所以在这一点上你已经改变了你的问题。在评论中,你说:

假设我只有一个注册类,对于当前设置的背景视图,有没有办法为一个部分而不是整个布局实现它?

当然,虽然我不知道“使用当前设置”是什么意思,因为你现在拥有的代码(当前设置)显然是错误的,或者你会得到你想要的结果。

无论如何,这很简单:在您的部分提供程序功能中,只给那个部分一个装饰视图,不要给任何其他部分一个装饰视图!

这是一个集合视图的截图,其中只有第一部分有背景装饰视图:

在此处输入图像描述

代码非常简单:

private func createLayout() -> UICollectionViewLayout {
    let lay = UICollectionViewCompositionalLayout { ix, env in
        // ... create item and group (omitted) ...
        let section = NSCollectionLayoutSection(group: group)
        // ... create header (omitted) ...
        // okay, create decoration!
        let deco = NSCollectionLayoutDecorationItem.background(elementKind: "background")
        deco.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)
        if ix == 0 { // only add the decoration for section 0
            section.decorationItems = [deco]
        }
        return section
    }
    return lay
}
于 2020-11-26T22:50:44.617 回答
0

问题是这对线:

layout.register(RedBackgroundSupplementaryView.self, forDecorationViewOfKind:"background")
layout.register(BlackBackgroundSupplementaryView.self, forDecorationViewOfKind:"background")

你不能两全其美。该kind字符串是您将如何识别您想要的:

let backgroundItem = NSCollectionLayoutDecorationItem.background(elementKind: "background")

但你只有一根kind弦。您的第二个注册出现并取代了第一个。您需要使用两种不同的背景颜色注册两种不同的装饰视图类型。

于 2020-11-26T21:04:19.893 回答