Swift 使用UICollectionViewFlowLayout模拟UITableView布局
我一直在尝试使用UICollectionView模拟UITableView布局。到目前为止,我已经做到了:Swift 使用UICollectionViewFlowLayout模拟UITableView布局,swift,uitableview,layout,uicollectionview,Swift,Uitableview,Layout,Uicollectionview,我一直在尝试使用UICollectionView模拟UITableView布局。到目前为止,我已经做到了: class TableViewLayout: UICollectionViewFlowLayout { override init() { super.init() minimumInteritemSpacing = 0 minimumLineSpacing = 6 scrollDirection = .vertic
class TableViewLayout: UICollectionViewFlowLayout {
override init() {
super.init()
minimumInteritemSpacing = 0
minimumLineSpacing = 6
scrollDirection = .vertical
estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
let currentBounds = collectionView?.bounds ?? .zero
return newBounds.width != currentBounds.width
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: rect) else { return [] }
let sectionInsets: CGFloat = sectionInset.left + sectionInset.right
let contentInsets: CGFloat = (collectionView?.contentInset.left ?? 0) + (collectionView?.contentInset.right ?? 0)
for attribute in attributes where attribute.frame.intersects(rect) {
attribute.size.width = collectionView!.readableContentGuide.layoutFrame.width - sectionInsets - contentInsets
}
return attributes
}
}
这非常有效,直到我将设备从横向旋转到纵向,之后布局挂起,表示单元格比实际项目大小宽
我是不是遗漏了一些琐碎的东西?我没有看到任何突出的问题。布局非常简单,只需添加文本量不足的标签。在这里可以看到它们
肖像画
景观
从横向绘制人像后出错
发生此错误时,您是否可以检查您的
应验证布局
返回的内容?它应该返回true
。它确实返回true