UICollectionViewCompositionalLayout 的后台视图在哪个方法中出队

Pav*_*nov 5 ios uicollectionview swift

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

    let background = NSCollectionLayoutDecorationItem.background(elementKind: "background")
    section.decorationItems = [background]
Run Code Online (Sandbox Code Playgroud)

后来注册了

    layout.register(RoundedView.self, forDecorationViewOfKind: "background")
Run Code Online (Sandbox Code Playgroud)

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

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

但它没有被调用

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

进行布局

@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
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Raf*_*sun 7

为了调用背景视图,您不能使用

let background = NSCollectionLayoutDecorationItem.background(elementKind: "background")
section.decorationItems = [background]
Run Code Online (Sandbox Code Playgroud)

我使用了一种不同的方式来调用它们并使它们成为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]
Run Code Online (Sandbox Code Playgroud)

在集合视图中注册视图(在您的情况下为RoundedView),例如页眉/页脚视图。

collectionView.register(ReusableViewBackgroundSupplimentaryView.self, forSupplementaryViewOfKind: ReusableViewBackgroundSupplimentaryView.elementKindSectionBackground, withReuseIdentifier: ReusableViewBackgroundSupplimentaryView.reuseIdentifier)
Run Code Online (Sandbox Code Playgroud)

现在您将在以下位置收到回调

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
/// Match your stupid identifier "background"
/// ReusableViewBackgroundSupplimentaryView.elementKindSectionBackground
}
Run Code Online (Sandbox Code Playgroud)

或现代可区分数据源

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

现在享受吧!