3 回答
TA貢獻1816條經驗 獲得超4個贊
更新SWIFT 4
systemLayoutSizeFittingSizesystemLayoutSizeFitting
為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}注preferredLayoutAttributesFittingAttributesisHeightCalculated
體驗你的布局
UITableView
警告
UndefinedtraitCollection
TA貢獻1818條經驗 獲得超11個贊
UICollectionViewFlowLayout.automaticSizeUICollectionViewFlowLayoutAutomaticSize
self.flowLayout.estimatedItemSize = CGSize(width: 100, height: 100)
self.flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
- 3 回答
- 0 關注
- 3122 瀏覽
添加回答
舉報
