swift - UICollectionViewFlowLayoutestimatedItemSize 不能在 iOS12 上正常工作,虽然它在 iOS 11 上工作正常。*
问题描述
对于我们使用的 UICollectionView 的动态高度单元格,
if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
在高度和宽度的适当约束下,它适用于 iOS 11.* 版本,但它会破坏并且不会使单元格在 iOS 12.0 中动态化
解决方案
就我而言,我通过向单元格的 contentView 显式添加以下约束来解决此问题。
class Cell: UICollectionViewCell {
// ...
override func awakeFromNib() {
super.awakeFromNib()
// Addresses a separate issue and prevent auto layout warnings due to the temporary width constraint in the xib.
contentView.translatesAutoresizingMaskIntoConstraints = false
// Code below is needed to make the self-sizing cell work when building for iOS 12 from Xcode 10.0:
let leftConstraint = contentView.leftAnchor.constraint(equalTo: leftAnchor)
let rightConstraint = contentView.rightAnchor.constraint(equalTo: rightAnchor)
let topConstraint = contentView.topAnchor.constraint(equalTo: topAnchor)
let bottomConstraint = contentView.bottomAnchor.constraint(equalTo: bottomAnchor)
NSLayoutConstraint.activate([leftConstraint, rightConstraint, topConstraint, bottomConstraint])
}
}
这些约束已经存在于单元格的 xib 中,但不知何故,它们对于 iOS 12 来说还不够。
建议collectionView.collectionViewLayout.invalidateLayout()
在各个地方调用的其他线程对我的情况没有帮助。
此处的示例代码:https ://github.com/larrylegend/CollectionViewAutoSizingTest
这将解决方法应用于https://medium.com/@wasinwiwongsak/uicollectionview-with-autosizing-cell-using-autolayout-in-ios-9-10-84ab5cdf35a2的教程中的代码:
推荐阅读
- django - 没有使用 Django import export 导入除 id 字段之外的字段
- sql - 如何从事务块中获取选择结果?
- apache - 启动乘客时缺少模块错误
- typescript - 对象属性的类型安全映射
- django - 更新/合并数据,而不是用 Django Rest Framework 中的 PUT 方法替换它
- excel - 继承时 VBA 类型不匹配(错误 13),用户来自
- mongodb - 如何在整个集合中进行仅总和查询?
- html - 如何使用 jinja/html 处理引号中的引号?
- c++ - 创建一个可以在类型和变量上调用的类型特征
- asp.net - Web API 项目中的 JWT 令牌