执行 -layoutSubviews 后仍然需要自动布局。 UICollectionView的-layoutSubviews的实现需要调用super

Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super

我使用 UICollectionView,默认 UICollectionViewFlowLayout。它适用于 iOS 8,但在 iOS 7.1 我得到

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super

我找到了这个 “Auto Layout still required after executing -layoutSubviews” with UITableViewCell subclass 但 none 的解决方案有效

另一个线索是我在 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

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

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