在UITableView的标题视图上使用NSLayoutConstraints

Ada*_*ter 16 nslayoutconstraint

我试图将UIView添加到UITableView的标题,然后,使用NSLayoutConstraints我想给它一个高度.

我查看了Apple Docs和WWDC 2012视频,但我无法在任何地方找到此特定错误!

我有以下代码:

- (UIImageView *)logoImageView
{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
        [_logoImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
    return _logoImageView;
}
Run Code Online (Sandbox Code Playgroud)

...在viewDidLoad中

UIView *tableHeaderView = [[UIView alloc] init];
tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO;

[tableHeaderView addSubview:self.logoImageView];

[self.tableView setTableHeaderView:tableHeaderView];

NSDictionary *constraintViews = @{@"tableView" : self.tableView, @"tableHeaderView" : tableHeaderView, @"logoImageView" : self.logoImageView};

[self.tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tableHeaderView(50)]"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:constraintViews]];
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,我收到以下错误:

2012-10-08 16:31:34.559 myApp[6934:c07] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776
2012-10-08 16:31:34.561 myApp[6934:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
*** First throw call stack:
(0x199b012 0x17c0e7e 0x199ae78 0x1256f35 0x7589ef 0x17d46b0 0x622fc0 0x61733c 0x622eaf 0x7f78cd 0x7401a6 0x73ecbf 0x73ebd9 0x73de34 0x73dc6e 0x73ea29 0x741922 0x7ebfec 0x738bc4 0x738dbf 0x738f55 0x741f67 0x705fcc 0x706fab 0x718315 0x71924b 0x70acf8 0x27e8df9 0x27e8ad0 0x1910bf5 0x1910962 0x1941bb6 0x1940f44 0x1940e1b 0x7067da 0x70865c 0x6d5d 0x2395)
libc++abi.dylib: terminate called throwing an exception
Run Code Online (Sandbox Code Playgroud)

Ada*_*ter 2

克服这个问题的最佳方法是使用 Interface Builder 设置 header UITableView,然后将 Outlet 添加到 height NSLayoutConstraint

  • 能否详细说明一下。不知道这意味着什么。 (3认同)