Search code examples
ioscollectionviewflowlayoutlayoutsubviews

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


I use UICollectionView with default UICollectionViewFlowLayout. It works on iOS 8, but on iOS 7.1 I get

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

I found this “Auto Layout still required after executing -layoutSubviews” with UITableViewCell subclass but none of the solution works

Another clue is that I add some views into the UICollectionView, and setup AutoLayout for that view

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);
    }];

This is what in my custom UICollectionView

@implementation FTGCollectionView

- (void)layoutSubviews {
    [super layoutSubviews];
    //[self layoutIfNeeded]; // Should not call as it cause collection view to not scroll
}

@end

Solution

  • I think it is an iOS 7 bug, instead of [self.collectionView addSubview:button]; I change to [self.view addSubview:button];, self.view is self.collectionView's parent view.

    So in iOS7 don't add subview to UICollectionView and use auto-layout for that subview