【问题标题】:Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super执行 -layoutSubviews 后仍需要自动布局。 UICollectionView的-layoutSubviews的实现需要调用super
【发布时间】: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


【解决方案1】:

我认为是 iOS 7 的 bug,而不是 [self.collectionView addSubview:button]; 我改为 [self.view addSubview:button];,self.view 是 self.collectionView 的父视图。

所以在 iOS7 中不要将子视图添加到 UICollectionView 并为该子视图使用自动布局

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 2012-09-18
    • 1970-01-01
    • 2015-07-09
    • 2012-11-17
    • 2013-07-28
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多