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

14,853

Solution 1

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

Solution 2

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.

Solution 3

[self.view layoutIfNeeded]

Hope this helps

Solution 4

for me is was this

self.tableView.backgroundView = self.emptyView;

I changed to this

    NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"8.0" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
    // OS version >= 8.0
    self.tableView.backgroundView = self.emptyView;
}else{
    [self.tableView.backgroundView addSubview:self.emptyView];
}

Solution 5

You also need to disable mask translation for the table view.

Share:
14,853

Related videos on Youtube

Doug Smith
Author by

Doug Smith

I'm a web designer playing around with iOS from England

Updated on June 21, 2022

Comments

  • Doug Smith
    Doug Smith over 1 year

    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.

    • Tancrede Chazallet
      Tancrede Chazallet about 10 years
      Not an expert, but I had this issue too, and I think it's because UITableView, implements it's own version of layoutSubviews (or another methods like it which is needed for auto layout). I up your question cause I would like to have the answer too :)
    • Tancrede Chazallet
      Tancrede Chazallet about 10 years
      I went back to frame for this, but it's really not convenient once everything is with autolayout...
  • ivan.mylyanyk
    ivan.mylyanyk about 9 years
    Please, add more details for your approach, and not only "one-liner" However, thank you for the answer.
  • AechoLiu
    AechoLiu over 8 years
    I encounter the same problem. My -(void)layoutSubViews of sub-class will modify some constraint constant. Call [super layoutSubviews] before modifying constraint will crash in iOS7 (but not 8).