【发布时间】:2015-11-10 20:02:08
【问题描述】:
我使用UICollectionView 和默认UICollectionViewFlowLayout。它适用于 iOS 8,但在 iOS 7.1 上我得到了
由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'自动布局仍然 执行 -layoutSubviews 后需要。 UICollectionView 的 -layoutSubviews 的实现需要调用super
我找到了这个“Auto Layout still required after executing -layoutSubviews” with UITableViewCell subclass,但没有一个解决方案有效
另一个线索是我在 UICollectionView 中添加了一些视图,并为该视图设置了 AutoLayout
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.collectionView addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.equalTo(self.collectionView);
make.height.mas_equalTo(30);
}];
这就是我的自定义 UICollectionView 中的内容
@implementation FTGCollectionView
- (void)layoutSubviews {
[super layoutSubviews];
//[self layoutIfNeeded]; // Should not call as it cause collection view to not scroll
}
@end
【问题讨论】:
-
您是否覆盖了 layoutSubviews?如果是这样,你有没有打电话给 super.layoutSubviews?如果没有,那么你在这里:)
-
我看到这可能发生在 UICollectionView、UITextField、UITableView、UINavigationBar,... 我的解决方法是禁用子视图(和大子子视图)的自动布局并手动设置框架
-
对于UICollectionView,我们可以在
UICollectionReusableView的layoutSubviews中添加子视图
标签: ios collectionview flowlayout layoutsubviews