[英]UICollectionView - iOS 10 - crash on iPhone 6 Plus simulator but works on real device
I've created an UICollectionView that works fine on small devices like iPhone 5s/SE/6/6s/7 on both simulator and real device, but I have a strange situation when it comes about iPhone 6Plus, iPhone 7Plus and any iPad version. 我创建了一个UICollectionView,它可以在模拟器和真实设备上的小型设备(如iPhone 5s / SE / 6 / 6s / 7)上正常运行,但是当涉及到iPhone 6Plus,iPhone 7Plus和任何iPad版本时,我的情况很奇怪。 It works fine on real device, but it crashes in simulator with the following error: 它可以在真实设备上正常运行,但是会在模拟器中崩溃,并显示以下错误:
Assertion failure in -[_UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:invalidationContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.5.2/UIFlowLayoutSupport.m:823 -[_ 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' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“ UICollectionViewFlowLayout内部错误”
What I found out so far is that if I remove the estimatedItemSize it works fine but the auto-layout doesn't anymore, but if I have that enabled it crashes in simulator. 到目前为止,我发现的是,如果删除了EstimatedItemSize,它可以正常工作,但是自动布局不再可用,但是如果我启用了它,则它会在模拟器中崩溃。
I am using the latest Xcode version and iOS 10. 我正在使用最新的Xcode版本和iOS 10。
Any ideas why it's crashing? 任何想法为什么它崩溃?
Calling invalidateLayout
before layout is a workaround for this issue. 在布局之前调用invalidateLayout
是解决此问题的方法。
In a UIViewController
subclass: 在UIViewController
子类中:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
or in a UIView
subclass: 或在UIView
子类中:
override func layoutSubviews() {
super.layoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
This is some strange bug in iOS10 with iPhone Plus devices only. 仅在使用iPhone Plus设备的iOS10中,这是一个奇怪的错误。 I've faced the same issue, my solution was to call layoutIfNeeded like this: 我遇到了同样的问题,我的解决方案是这样调用layoutIfNeeded:
func numberOfSections(in collectionView: UICollectionView) -> Int { collectionView.layoutIfNeeded() // Patch: only to solve UIKit crash on Plus models return 1 }
Doing the same thing in different UICollectionViewDataSources methods will work as well 在不同的UICollectionViewDataSources方法中执行相同的操作也将起作用
Answer that worked for me based on orxelm 's answer on collectionViewContentSize in iOS 10 using self-sizing cells 基于orxelm 在iOS 10中使用自调整大小的单元格对collectionViewContentSize的回答为我解决的问题
Invalidating the layout seemed to prevent my collectionView from using my flow layout, and instead used the default layout. 使布局无效似乎阻止了我的collectionView使用我的流程布局,而是使用了默认布局。 This forced all my cells to be the same size which was not the desired outcome (for me anyway). 这迫使我所有细胞的大小都相同,这并不是理想的结果(无论如何对我而言)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.