我正在尝试在我的 collectionView 中实现自定义页 footer 分,但我想我遗漏了一些东西。我要存档我的代码。我希望你能帮助我。
谢谢!!!
自定义页 footer 分
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")
}
Collection View 类
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
}
最佳答案
添加此方法-:
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForFooterInSection section: Int) -> CGSize
关于ios - 我的 collectionView 中的页 footer 分出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45000181/