将标签设置为UITableView的tableFooterView - 使用autolayout

Z S*_*Z S 6 objective-c uitableview uikit nslayoutconstraint nsautolayout

当我的表视图为空时,我想在tableview页脚中添加一个简单的标签,以显示"无内容"消息.当我设置UILabel的框架和我用于footerView的UIView时它工作正常,但现在我正在尝试转换为使用自动布局,我无法让它工作.

这是我正在做的事情:

// Create the footer    
UIView *tempFooter = [[UIView alloc] init];
UILabel *atext = [[UILabel alloc] init];
tempFooter.translatesAutoresizingMaskIntoConstraints = NO;
atext.translatesAutoresizingMaskIntoConstraints = NO;

atext.numberOfLines = 0;
[atext setText:@"Add Some Text"];
atext.textAlignment = NSTextAlignmentCenter;
atext.font = [UIFont italicSystemFontOfSize:14];
atext.textColor = [UIColor grayColor];
atext.backgroundColor = [UIColor clearColor];
atext.lineBreakMode = NSLineBreakByWordWrapping;

// add label to footer view, and add constraints
[tempFooter addSubview:atext];
NSLayoutConstraint *tvatt1 = [NSLayoutConstraint constraintWithItem:tempFooter
                                                          attribute:NSLayoutAttributeLeft
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:atext attribute:NSLayoutAttributeLeft
                                                         multiplier:1.0
                                                           constant:10.0];

NSLayoutConstraint *tvatt2 = [NSLayoutConstraint constraintWithItem:tempFooter
                                                          attribute:NSLayoutAttributeRight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:atext
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1.0
                                                           constant:10.0];

NSLayoutConstraint *tvatt3 = [NSLayoutConstraint constraintWithItem:tempFooter
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:atext
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:5.0
                                                           constant:0];

NSLayoutConstraint *tvatt4 = [NSLayoutConstraint constraintWithItem:tempFooter
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:atext
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant: 0];

[tempFooter addConstraints: @[tvatt1, tvatt2, tvatt3, tvatt4]];


// set the footerview 
self.tview.tableFooterView = tempFooter;
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我遇到了崩溃:

2014-09-08 19:57:16.594 SimpleList [49442:60b] *断言失败 - [UITableView layoutSublayersOfLayer:],/ SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794 2014-09-08 19:57: 21.073 SimpleList [49442:60b]*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'执行-layoutSubviews后仍需要自动布局.UITableView的-layoutSubviews实现需要调用super

我不确定我做错了什么.我也尝试了设置约束self.tview.tableFooterViewWRT的tempFooter(寄托对所有4个方面),但不改变崩溃.我也打来电话:

[self.tview.tableFooterView layoutSubviews];
Run Code Online (Sandbox Code Playgroud)

但这也无济于事.

我有什么想法我做错了吗?

rde*_*mar 13

对于表视图,它们的单元格或表格页眉和页脚视图(或者对于控制器的self.view),您不应将translatesAutoresizingMaskIntoConstraints设置为NO.可以对这些视图中的视图使用约束(单元格的内容视图或页眉和页脚视图).我向viewDidLoad添加了一个简化版本的代码,它按预期工作.请注意,您需要为页脚视图指定高度和x位置(忽略y位置和宽度),

    UIView *tempFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
    UILabel *atext = [[UILabel alloc] init];
    atext.translatesAutoresizingMaskIntoConstraints = NO;
    atext.numberOfLines = 0;
    [atext setText:@"Add Some Text"];
    atext.textAlignment = NSTextAlignmentCenter;
    atext.font = [UIFont italicSystemFontOfSize:14];
    atext.textColor = [UIColor grayColor];
    atext.backgroundColor = [UIColor clearColor];
    atext.lineBreakMode = NSLineBreakByWordWrapping;

    [tempFooter addSubview:atext];
    NSDictionary *dict = NSDictionaryOfVariableBindings(atext);
    [tempFooter addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[atext]-10-|" options:0 metrics:nil views:dict]];
    [tempFooter addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[atext]|" options:0 metrics:nil views:dict]];
    self.tableView.tableFooterView = tempFooter;
Run Code Online (Sandbox Code Playgroud)

我不确定你的约束是什么,tvatt3.乘数(你有5个)对顶部或左边约束没有任何作用,因为这些属性的值是0.我使用了视觉约束,而不是你使用的方法,但是当我使用你的代码添加约束时,它也有效.