问题描述
由于我添加了以下代码,每次我的应用打开此 UITableViewController
时它都会崩溃:
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]];
它给了我这个错误:
* 由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:执行 -layoutSubviews 后仍需要自动布局.UITableView的-layoutSubviews的实现需要调用super.'
我到底做错了什么?当有 0 行时,我在 tableView:numberOfRowsInSection:
中调用该代码.
What on earth am I doing wrong? I call that code in tableView:numberOfRowsInSection:
when there's 0 rows.
推荐答案
我正在继承 UIScrollView 并在 iOS 7(但不是 8)上收到相同的错误消息.
I was subclassing UIScrollView and received the same error message on iOS 7 (but not 8).
我以类似于以下方式覆盖 layoutSubviews:
I was overriding layoutSubviews in a manner similar to the following:
- (void)layoutSubviews {
[super layoutSubviews];
// code to scroll the view
}
我解决了这个问题,把对 super 的 layoutSubviews 的调用移到方法中的最后一件事:
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];
}
这篇关于为什么我得到一个“执行 -layoutSubviews 后仍然需要自动布局"?每次我的应用程序现在启动时都会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!