layoutSubviews&QUOT后

layoutSubviews&QUOT后

本文介绍了为什么我得到一个"自动布局仍然需要执行-layoutSubviews&QUOT后;每一个错误,现在我的应用程序启动的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我加入了以下code,每次我的应用程序打开该的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的需要调用超。

这到底我做错了什么?我称之为的code的tableView:numberOfRowsInSection:当有0行

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
}

我通过移动电话到超的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&QUOT后;每一个错误,现在我的应用程序启动的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:13