gpt4 book ai didi

ios - UICollectionViewCompositionalLayout 的背景 View 在哪个方法中出队

转载 作者:行者123 更新时间:2023-12-01 23:35:33 28 4
gpt4 key购买 nike

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

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

后来注册

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

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

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

但它没有被调用

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

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
}

谢谢

最佳答案

为了调用后台 View ,您不能使用

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]

在像页眉/页脚 View 这样的 Collection View 中注册 View (RoundedView)。

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(...)
}

现在享受吧!

关于ios - UICollectionViewCompositionalLayout 的背景 View 在哪个方法中出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65700940/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com