【发布时间】:2020-03-08 04:40:40
【问题描述】:
我正在尝试根据给定的文本创建具有半宽和动态高度的集合视图 (Expected View)。
我正在尝试通过(ViewController 中的代码)实现这一目标
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.estimatedItemSize = CGSize(width: (UIScreen.main.bounds.width / 2), height: 2000.0)
flowLayout.itemSize = UICollectionViewFlowLayout.automaticSize
And(UICollectionviewCell 中的代码)
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var frame = layoutAttributes.frame
frame.size.height = ceil(size.height)
layoutAttributes.frame = frame
return layoutAttributes
}
通过使用上面的代码,我可以实现创建collectionviewActual view(半宽和动态高度)。但集合视图单元格高度不等于相邻。实际上,此集合视图单元格高度应与相邻单元格高度(具有最大高度)相同。如何根据相邻单元格大小更新单元格大小?
提前致谢
【问题讨论】:
-
为什么要设置 UICollectionViewFlowLayout.automaticSize,评论这行试试看
-
这一行就够了 flowLayout.estimatedItemSize = CGSize(width: (UIScreen.main.bounds.width / 2), height: 2000.0)
-
@YagneshDobariya:不走运。
-
你能解决这个问题吗?
标签: ios swift collectionview