我创建了一个 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/