为什么每次我的应用程序启动时都会出现"执行-layoutSubviews后仍然需要自动布局"错误?

Dou*_*ith 18 objective-c uitableview ios autolayout nslayoutconstraint

由于我添加了以下代码,每次我的应用程序打开UITableViewController它时都会崩溃:

 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]];    
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误:

*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'执行-layoutSubviews后仍需要自动布局.UITableView的-layoutSubviews实现需要调用super.

我究竟做错了什么?tableView:numberOfRowsInSection:当有0行时我会调用该代码.

Sen*_*ful 14

我是UIScrollView的子类,并在iOS 7上收到了相同的错误消息(但不是8).

我以类似于以下的方式覆盖layoutSubviews:

- (void)layoutSubviews {
    [super layoutSubviews];
    // code to scroll the view
}
Run Code Online (Sandbox Code Playgroud)

我通过将对super的layoutSubviews的调用移动到方法中的最后一件事来解决了这个问题:

- (void)layoutSubviews {
    // code to scroll the view
    [super layoutSubviews];
}
Run Code Online (Sandbox Code Playgroud)


Coc*_*ics 1

您还需要禁用表视图的掩码转换。