iOS:UITableView底部的额外空间

时间:2016-03-15 08:38:40

标签: ios objective-c uitableview

我已经以编程方式创建了UITableView,但它下面有一些额外的空间(准确地说是29个像素)。

这是我初始化表格的方式:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
[tableView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:tableView];
self.alertsTableView = tableView;

以下是限制因素:

    @"V:|-10-[_alertsTableView(0)]-5-[_optionsLabel]",
    @"H:|-15-[_alertsTableView]-15-|", 

我尝试将页脚视图设置为nil并为其高度返回零:

[self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

我会根据内容动态更改其高度:

- (void)updateHeight:(CGFloat)height {
    if (self.tableView.superview) {
        NSLayoutConstraint *heightConstraint;
        for (NSLayoutConstraint *constraint in self.tableView.superview.constraints) {
            if (constraint.firstAttribute == NSLayoutAttributeHeight) {
                heightConstraint = constraint;
                break;
            }
        }
        heightConstraint.constant = height;
    }
}

我还将setAutomaticallyAdjustsScrollViewInsets设置为NOthis是最终结果。单元格和空白区域之间有一个分隔符,但底部不是单元格。

2 个答案:

答案 0 :(得分:9)

您可以使用contentInset来补偿29px的额外底部边距:

tableView.contentInset = UIEdgeInsetsMake(0, 0, -29, 0);

答案 1 :(得分:0)

尝试添加TableViewFooterView

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
相关问题