smeshko
smeshko

Reputation: 1226

iOS:Extra space at the bottom of UITableView

I have created UITableView programmatically, but it has some extra space below (29 pixels to be precise).

This is how I initialize the table:

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

Here are the constraints:

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

I tried setting the footer view to nil and returning zero for its height:

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

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

I dynamically change its height, based on the content:

- (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;
    }
}

I also set setAutomaticallyAdjustsScrollViewInsets to NO. And this is the end result. There is a separator between the cell and the empty space, but the bottom is not a cell.

Upvotes: 6

Views: 8556

Answers (2)

user3168555
user3168555

Reputation: 91

Try adding a TableViewFooterView.

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

Upvotes: 8

user5938635
user5938635

Reputation:

You can use contentInset to compensate the 29px extra bottom margin:

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

Upvotes: 13

Related Questions