Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 240
onmyway133 Avatar asked Jan 27 '26 17:01

onmyway133


1 Answers

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

like image 185
Qiulang 邱朗 Avatar answered Jan 30 '26 22:01

Qiulang 邱朗



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!