3 回答
TA贡献1816条经验 获得超4个赞
更新SWIFT 4
systemLayoutSizeFittingSize
systemLayoutSizeFitting
为IOS 9更新
TL;DR
1.成套 estimatedItemSize
在……上面 UICollectionViewFlowLayout
estimatedItemSize
self.flowLayout.estimatedItemSize = CGSize(width: 100, height: 100)
2.在单元格子类上添加对调整大小的支持。
preferredLayoutAttributesFittingAttributes
.
使用自动布局创建和配置单元格
awakeFromNib
).
实施 preferredLayoutAttributesFittingAttributes
在您的自定义单元格中
cellForItem
//forces the system to do one layout passvar isHeightCalculated: Bool = falseoverride func preferredLayoutAttributesFittingAttributes (_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { //Exhibit A - We need to cache our calculation to prevent a crash. if !isHeightCalculated { setNeedsLayout() layoutIfNeeded() let size = contentView.systemLayoutSizeFitting(layoutAttributes.size) var newFrame = layoutAttributes.frame newFrame.size.width = CGFloat(ceilf(Float(size.width))) layoutAttributes.frame = newFrame isHeightCalculated = true } return layoutAttributes}
注preferredLayoutAttributesFittingAttributes
isHeightCalculated
体验你的布局
UITableView
警告
Undefined
traitCollection
TA贡献1818条经验 获得超11个赞
UICollectionViewFlowLayout.automaticSize
UICollectionViewFlowLayoutAutomaticSize
self.flowLayout.estimatedItemSize = CGSize(width: 100, height: 100)
self.flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
- 3 回答
- 0 关注
- 3005 浏览
添加回答
举报