gpt4 book ai didi

ios - UICollectionView 自定义布局页脚 View 不粘在 Collection View 的底部

转载 作者:行者123 更新时间:2023-11-29 05:29:52 25 4
gpt4 key购买 nike

我正在关注this guide为了制作类似 Pinterest 的 Collection View 布局,它工作得很好,直到我想向它添加页脚 View 。

prepare() 方法中,我添加了以下代码:

// Add Attributes for section footer
let footerAtrributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, with: IndexPath(item: 0, section: 0))
footerAtrributes.frame = CGRect(x: 0, y: collectionView.bounds.maxY, width: UIScreen.main.bounds.width, height: myFooterHeight)
cache.append(footerAtrributes)

调用collectionView:viewForSupplementaryElementOfKind:atIndexPath:后,我可以将页脚添加到collectionView,但它不在正确的y位置。

像这样:

like this

自从我调用collectionView.reloadData()以来,如果进一步滚动它就会崩溃。我收到的错误是:

layout attributes for supplementary item at index path changed without invalidating the layout.

我认为 y 位置不正确,但我真的不知道该放什么。

随着 UICollectionView 更新(加载更多项目),如何将其设置到正确的位置?

最佳答案

此错误:

layout attributes for supplementary item at index path changed without invalidating the layout.

是为FooterView设置布局的原因

所以你必须为 View 进行布局

这是我的CollectionView的示例

let historyCollection: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 20
layout.scrollDirection = .vertical

let historyCollection = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
historyCollection.register(CodiHistoryCell.self, forCellWithReuseIdentifier: "history")
historyCollection.register(fooderSection.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")
historyCollection.isScrollEnabled = true
historyCollection.backgroundColor = UIColor.clear
historyCollection.translatesAutoresizingMaskIntoConstraints = false
return historyCollection
}()

所以在你命名你的标识符之后

UICollectionElementKindSectionFooter, withReuseIdentifier: "footer"

你必须将它放在我在 Controller 类中所做的“FooterView”中

import UIKit

class fooderSection: UICollectionReusableView {

let checkBtn: UIButton = {
let checkBtn = UIButton(type: .custom)
checkBtn.setImage(UIImage(named: "loadMoreIcon"), for: .normal)
checkBtn.contentMode = UIView.ContentMode.scaleAspectFit
checkBtn.translatesAutoresizingMaskIntoConstraints = false
return checkBtn
}()



required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override init(frame: CGRect) {
super.init(frame: frame)

self.addSubview(checkBtn)

//this is how it's going to show in the bottonVIEW
just play while this values
NSLayoutConstraint.activate([
checkBtn.centerXAnchor.constraint(equalTo: self.centerXAnchor),
checkBtn.centerYAnchor.constraint(equalTo: self.centerYAnchor),
checkBtn.widthAnchor.constraint(equalToConstant: 70),
checkBtn.heightAnchor.constraint(equalToConstant: 70)
])

}
}

您还必须为 FooterView 设置引用尺寸

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: interface.frame.height * 0.1)
}

关于ios - UICollectionView 自定义布局页脚 View 不粘在 Collection View 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728337/

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