我创建了一个非常简单的测试用例来重现这个问题。
我正在尝试以编程方式将页脚视图设置为表格视图。请注意,我指的是表格视图最底部的页脚 - 而不是部分页脚(大多数堆栈溢出答案都会使他们感到困惑)。
这是我的非常简单的代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *footerContainer = [[UIView alloc] initWithFrame:CGRectZero];
footerContainer.backgroundColor=[UIColor greenColor];
footerContainer.translatesAutoresizingMaskIntoConstraints=NO;
[footerContainer addConstraints:@[[NSLayoutConstraint
constraintWithItem:footerContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100
],
[NSLayoutConstraint
constraintWithItem:footerContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:[UIScreen mainScreen].bounds.size.width
]]];
self.mytableview.tableFooterView=footerContainer;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}
然而,结果看起来像这样:
正如您所注意到的,页脚显示在表格视图的顶部。这是一个错误还是我错过了什么?
如果我将 tableFooterView 更改为 tableHeaderView,那么它就可以正常工作。所以我希望页脚也能有同样的效果,但事实并非如此。
不设置translatesAutoresizingMaskIntoConstraints = false
on a UIView
被分配给tableView.tableFooterView
.
在你的情况下,这是footerContainer
。我也有同样的问题。在对该问题的评论中提到了这一点,但在我注意到我这样做之前,我仍然花了几个小时进行故障排除,所以我也将其放在这里作为可能的答案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)