Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a "Auto Layout still required after executing -layoutSubviews" error every time my app launches now?

Since I added the following code, every time my app opens this UITableViewController it crashes:

 self.noArticlesView = [[UIView alloc] init];
 self.noArticlesView.translatesAutoresizingMaskIntoConstraints = NO;
 self.noArticlesView.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.961 alpha:1];

 [self.view addSubview:self.noArticlesView];

 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];    

And it gives me this error:

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

What on earth am I doing wrong? I call that code in tableView:numberOfRowsInSection: when there's 0 rows.

like image 857
Doug Smith Avatar asked Nov 07 '13 13:11

Doug Smith


2 Answers

I was subclassing UIScrollView and received the same error message on iOS 7 (but not 8).

I was overriding layoutSubviews in a manner similar to the following:

- (void)layoutSubviews {
    [super layoutSubviews];
    // code to scroll the view
}

I resolved the issue by moving the call to super's layoutSubviews to be the last thing in the method:

- (void)layoutSubviews {
    // code to scroll the view
    [super layoutSubviews];
}
like image 111
Senseful Avatar answered Nov 15 '22 19:11

Senseful


Had the same problem. Added view(s) to self.tableView and used constraints. Do not add the views to the table view via addSubview: but add them as header(s), footer(s) or cells.

like image 36
dergab Avatar answered Nov 15 '22 19:11

dergab