我仔细地按照exemple来转换我的collectionView,以便在使用Nib时使用动态高度。
在此viewDidLoad
中设置 estimatedItemSize 之后:
override func viewDidLoad() {
super.viewDidLoad()
if let layout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
}
我得到了这个例外:
2017-09-14 16:03:06.686930-0400 ClientCenterApp[404:48958] *** Assertion failure in -[_UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:invalidationContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.7.47/UIFlowLayoutSupport.m:823
2017-09-14 16:03:06.689177-0400 ClientCenterApp[404:48958] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionViewFlowLayout internal error'
任何人都知道为什么?
答案 0 :(得分:2)
这解决了我的问题:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}