【发布时间】:2014-09-09 03:01:39
【问题描述】:
当我的表格视图为空时,我想在表格视图页脚中添加一个简单的标签以显示“无内容”消息。当我设置用于 footerView 的 UILabel 和 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;
当我运行它时,我遇到了崩溃:
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.tableFooterView wrt 和tempFooter 设置约束(固定在所有4 面),但这不会改变崩溃。我也叫:
[self.tview.tableFooterView layoutSubviews];
但这也无济于事。
任何想法我做错了什么?
【问题讨论】:
-
尝试注释掉这一行,tempFooter.translatesAutoresizingMaskIntoConstraints = NO;看看是否能解决问题。
-
不,不幸的是导致不同的崩溃:2014-09-08 22:46:19.905 无法同时满足约束。可能以下列表中的至少一个约束是您不想要的...... ( "<0x117137d80 v:><0x117082480 h="--" v="--&" v:>0x117082480>0x117137d80>
标签: objective-c uitableview uikit nslayoutconstraint autolayout