我想在我的UICollectionViewCompositionalLayout
岁时创建装饰视图.我使用以下代码:
func createListTableSection(using section: Section) -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1/3), heightDimension: .fractionalHeight(1))
let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8)
let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .fractionalWidth(0.5))
let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize, subitems: [layoutItem])
let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
layoutSection.orthogonalScrollingBehavior = .groupPagingCentered
layoutSection.decorationItems = [NSCollectionLayoutDecorationItem.background(elementKind: RoundedBackgroundView.reuseIdentifier)]
let layout = UICollectionViewCompositionalLayout(section: layoutSection)
layout.register(RoundedBackgroundView.self, forDecorationViewOfKind: RoundedBackgroundView.reuseIdentifier)
return layoutSection
}
圆形背景视图
class RoundedBackgroundView: UICollectionReusableView {
static let reuseIdentifier = "RoundedBackgroundView"
private var insetView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .secondarySystemFill
view.layer.cornerRadius = 15
view.clipsToBounds = true
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
addSubview(insetView)
NSLayoutConstraint.activate([
insetView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
trailingAnchor.constraint(equalTo: insetView.trailingAnchor, constant: 15),
insetView.topAnchor.constraint(equalTo: topAnchor),
insetView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我得到了这个错误:
线程1:"无法将种类的装饰视图出列: 必须注册为类或nib或连接 故事板中的原型"
如何修复它?