UICollectionViewCompositionalLayout 的背景视图在哪个方法中出队

In which method is background view dequeued for UICollectionViewCompositionalLayout

我用UICollectionViewCompositionalLayout。所有部分都有圆形背景。背景只是添加到所有部分

    let background = NSCollectionLayoutDecorationItem.background(elementKind: "background")
    section.decorationItems = [background]

后来注册

    layout.register(RoundedView.self, forDecorationViewOfKind: "background")

问题是:如何改变背景视图的颜色?是否有视图出队的功能?我已经试过了

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    preconditionFailure("")
}

但是没有调用

所有相关代码: 相当愚蠢的背景视图

final class RoundedView: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        config()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        config()
    }

    private func config() {
        self.layer.cornerRadius = Constant.Dimens.cornerRadius
        self.clipsToBounds = true
    }
}

制作布局

@available(iOS 13, *)
private static func makeLayout() -> UICollectionViewLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(100))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)

    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(200))
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
    let section = NSCollectionLayoutSection(group: group)

    let background = NSCollectionLayoutDecorationItem.background(elementKind: "background")
    section.decorationItems = [background]

    let layout = UICollectionViewCompositionalLayout(section: section)
    layout.register(RoundedView.self, forDecorationViewOfKind: "background")
    return layout
}

谢谢

为了调用后台视图,您不能使用

let background = NSCollectionLayoutDecorationItem.background(elementKind: "background")
section.decorationItems = [background]

我使用了一些不同的方式来调用它们并使它们成为 boundarySupplementaryItems

///Background
let sectionBackgroundSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: groupSize.heightDimension)
let sectionBackgroundAnchor = NSCollectionLayoutAnchor(edges: [.all])
let sectionBackground = NSCollectionLayoutBoundarySupplementaryItem(
            layoutSize: sectionBackgroundSize,
            elementKind: ReusableViewBackgroundSupplimentaryView.elementKindSectionBackground, /// "background"
            containerAnchor: sectionBackgroundAnchor)
section.boundarySupplementaryItems = [sectionBackground]

在集合视图中注册视图(RoundedView 在你的情况下)像 header/footer 视图。

collectionView.register(ReusableViewBackgroundSupplimentaryView.self, forSupplementaryViewOfKind: ReusableViewBackgroundSupplimentaryView.elementKindSectionBackground, withReuseIdentifier: ReusableViewBackgroundSupplimentaryView.reuseIdentifier)

现在您将在

中收到回电
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
/// Match your stupid identifier "background"
/// ReusableViewBackgroundSupplimentaryView.elementKindSectionBackground
}

或现代可微分数据源

dataSource?.supplementaryViewProvider = { (
            collectionView: UICollectionView,
            kind: String,
            indexPath: IndexPath) -> UICollectionReusableView? in
   /// Match your stupid identifier "background"
    /// ReusableViewBackgroundSupplimentaryView.elementKindSectionBackground
    /// return collectionView.dequeueSuplementaryView(...)
    }

现在享受吧!