ios - UICollectionView - iOS 10 - 在 iPhone 6 Plus 模拟器上崩溃,但在真实设备上运行

标签 ios swift autolayout uicollectionview ios10

我创建了一个 UICollectionView,它在模拟器和真实设备上的 iPhone 5s/SE/6/6s/7 等小型设备上运行良好,但当涉及 iPhone 6Plus、iPhone 7Plus 和任何设备时,我遇到了奇怪的情况iPad 版本。它在真实设备上运行良好,但在模拟器中崩溃并出现以下错误:

Assertion failure in -[_UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:invalidationContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.5.2/UIFlowLayoutSupport.m:823

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionViewFlowLayout internal error'

到目前为止我发现,如果我删除estimatedItemSize,它工作正常,但自动布局不再起作用,但如果我启用它,它会在模拟器中崩溃。

我使用的是最新的 Xcode 版本和 iOS 10。

知道为什么会崩溃吗?

最佳答案

在布局之前调用 invalidateLayout 是解决此问题的方法。

UIViewController 子类中:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    collectionView.collectionViewLayout.invalidateLayout()
}

或者在UIView子类中:

override func layoutSubviews() {
    super.layoutSubviews()
    collectionView.collectionViewLayout.invalidateLayout()
}

关于ios - UICollectionView - iOS 10 - 在 iPhone 6 Plus 模拟器上崩溃,但在真实设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56019779/

相关文章:

ios - 可以从 UISearchBar 中删除背景或边框吗?

ios - 使用内部应用程序凭据而不是用户凭据对 iOS 应用程序进行身份验证

ios - 如何以编程方式将下一个 View Controller 推送到导航 Controller 上?

ios - 快速动态添加按钮

c# - WPF - 在没有设计器的情况下创建实体布局

iphone - 如何将 AVAsset 旋转到纵向模式?

ios - CGAffineTransform 和 NSLayoutConstraint 冲突

ios - 在快速关闭中返回 Void 和 () 之间的区别

ios - 在 UIViewController、iOS Swift4.2、Xcode10.1 中只调用一次函数

ios - NSLayoutConstraint subview 不会自动调整大小以适应容器 View