gpt4 book ai didi

ios - UICollectionViewLayout 和方法 layoutAttributesForItem 从未被调用

转载 作者:技术小花猫 更新时间:2023-10-29 11:05:11 29 4
gpt4 key购买 nike

我正在尝试实现 UICollectionViewFlowLayoutUICollectionViewLayout - 永远不会调用 layoutAttributesForItem 的任何方式。

我从其他人那里看到他们从 self.layoutAttributesForItem 调用了 layoutAttributesForItem

this flowout示例:

我创建了一个 Playground你可以看看的项目。总的来说,我只是想扩大中心 View 。

最佳答案

Try to use it by Sub-Classing UICollectionViewFlowLayout

let flowLayout = LeftAgignedFlowLayout()
flowLayout.minimumLineSpacing = 18
flowLayout.minimumInteritemSpacing = 8
flowLayout.scrollDirection = .vertical
self.collectionViewLayout = flowLayout



class LeftAgignedFlowLayout : UICollectionViewFlowLayout {

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let arr = super.layoutAttributesForElements(in: rect)!
return arr.map {
atts in

var atts = atts
if atts.representedElementCategory == .cell {
let ip = atts.indexPath
atts = self.layoutAttributesForItem(at:ip)!
}
return atts
}
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
var atts = super.layoutAttributesForItem(at:indexPath)!
if indexPath.item == 0 {
return atts

}
if atts.frame.origin.x - 1 <= self.sectionInset.left {
return atts

}
let ipPv = IndexPath(item:indexPath.row-1, section:indexPath.section)
let fPv = self.layoutAttributesForItem(at:ipPv)!.frame
let rightPv = fPv.origin.x + fPv.size.width + self.minimumInteritemSpacing
atts = atts.copy() as! UICollectionViewLayoutAttributes
atts.frame.origin.x = rightPv
return atts
}

}

关于ios - UICollectionViewLayout 和方法 layoutAttributesForItem 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129682/

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