我正在尝试对右上角的按钮进行编码,以便在点击时,它只会在白条中显示姓名,而不显示照片或卡片上人物的位置。这是一张说明展开部分的照片。
我尝试使用sizeForItemAt
in来操纵大小collectionView
,但这不起作用。这是我所做的:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
if expand {
cell?.profileimage.alpha = 0
cell?.poscomp.alpha = 0
return CGSize(width: 360, height: 208)
} else {
cell?.profileimage.alpha = 1
cell?.poscomp.alpha = 1
return CGSize(width: 360, height: 40)
}
}
对于按钮,我做了...
@IBAction func collapse(_ sender: Any) {
expand = !expand
for index in 0...associates.count {
cardCollectionView.reloadItems(at: [IndexPath(row: 0, section: index)])
}
}
是否有一种正确/更简单的方法来操作 CollectionViewCell 中的高度和元素?
提前致谢。
你正在做正确的代码,但我不承认你的问题。添加以下工作正常的代码,这可能是您的期望。
var expandedHeight : CGFloat = 200
var notExpandedHeight : CGFloat = 50
var isExpanded = false
在按钮的动作方法中添加代码以collectionView
使用/不使用动画重新加载
@IBAction func btnExpandTapped(_ sender: UIButton) {
self.isExpanded = !self.isExpanded
// Simple reload
// self.cvList.reloadData()
// Reload with animation
for i in 0..<5 {
let indexPath = IndexPath(item: i, section: 0)
UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: .curveEaseInOut, animations: {
self.cvList.reloadItems(at: [indexPath])
}, completion: { success in
print("success")
})
}
}
添加UICollectionViewDelegateFlowLayout
的sizeForItemAt
方法并添加以下代码以更改集合视图单元格的高度。
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if isExpanded {
return CGSize(width: collectionView.frame.width, height: 200)
} else {
return CGSize(width: collectionView.frame.width, height: 40)
}
}
}
检查以下链接以获取演示应用程序。
https://freeupload.co/files/02c31165b3c6876678301ae075f5722b.zip
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句