我创建了一个非常简单的测试用例来重现此问题。UITableView tableFooterView显示在UITableView的顶部 - 错误
我想以编程方式设置页脚视图到tableview。请注意,我指的是在tableview的最底部的页脚 - 而不是页脚(大多数堆栈溢出的答案让他们感到困惑)。
这里是我的代码非常简单:
- (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;
}
然而,结果是这样的:
你可能注意到了,页脚显示上的tableview的顶部。这是一个错误还是我错过了什么?
如果我将tableFooterView更改为tableHeaderView,那么它工作正常。所以我期待着同样的脚步也能工作,但事实并非如此。
设置'translatesAutoresizingMaskIntoConstraints = NO'很可能会破坏tableview用于定位页脚的任何方法。 – dan
您是否使用约束,因为您将(或可能)在页脚视图中具有动态内容?如果是这样,我可以给你一个解决方案...... – DonMag
@DonMag是的,我一直在寻找一种解决方案,它将使用约束来设置它,而不是手动设置框架。我的实际应用具有动态大小的页脚内容。 –