我想在我的UICollectionViewCompositionalLayout
岁时创建装饰视图.我使用以下代码:
func createCompositionalLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
let section = self.sections[sectionIndex]
switch section.type {
case "mediumTable":
return self.createMediumTableSection(using: section)
case "smallTable":
return self.createSmallTableSection(using: section)
case "listTable":
return self.createListTableSection(using: section)
default:
return self.createFeaturedSection(using: section)
}
}
layout.register(RoundedBackgroundView.self, forDecorationViewOfKind: RoundedBackgroundView.reuseIdentifier)
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
self.view = collectionView
let config = UICollectionViewCompositionalLayoutConfiguration()
config.interSectionSpacing = 20
layout.configuration = config
return layout
}
圆形背景视图
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")
}
}
但我看不到背景
如何修复它?