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

Doug Smith picture Doug Smith · Nov 7, 2013 · Viewed 14.8k times · Source

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.

Answer

Senseful picture Senseful · Feb 14, 2015

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