我仔细地跟着那个exemple来转换我的collectionView,使用动态高度同时使用Nibs。
在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'
任何人都知道为什么?
这解决了我的问题:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}