我正在尝试在我的collectionView中实现自定义页脚部分,但我想我错过了一些东西。我要存档我的代码。我希望你能帮助我。
感谢!!!
自定义页脚部分
class FooterCell: UICollectionViewCell {
let etiqueta: UILabel = {
let label = UILabel()
label.backgroundColor = .green
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(etiqueta)
etiqueta.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
etiqueta.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
etiqueta.widthAnchor.constraint(equalToConstant: 50).isActive = true
etiqueta.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
集合视图类
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(FooterCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer") }
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableView = FooterCell()
if kind == UICollectionElementKindSectionFooter {
reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath) as! FooterCell
reusableView.etiqueta.backgroundColor = .green
}
return reusableView
}
答案 0 :(得分:1)
添加此方法 - :
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForFooterInSection section: Int) -> CGSize