作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我正在尝试实现 UICollectionViewFlowLayout
或 UICollectionViewLayout
- 永远不会调用 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/
我正在尝试实现 UICollectionViewFlowLayout 或 UICollectionViewLayout - 永远不会调用 layoutAttributesForItem 的任何方式。
我是一名优秀的程序员,十分优秀!