2016-03-15 82 views
1

我以編程方式創建了UITableView,但它在下面有一些額外空間(精確爲29像素)。iOS:UITableView底部的額外空間

這是我的初始化表:

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-|", 

我試過頁腳視圖設置爲零,併爲它的高度返回零:

[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設置爲NO。並且this是最終結果。單元格和空白空間之間有一個分隔符,但底部不是單元格。

+1

您還可以設置footerview使用。 table.tableFooterView = [uiview new]; –

+0

嘗試「Debug View Hierarchy」按鈕 - 可以幫助您查看哪個UIView佔用了空間。 – siburb

+0

你可以分享代碼如何計算「高度」。此外,我會建議改變方法如何更新約束。而不是運行所有的約束條件,你可以在第一時間擁有一個用於高度約束的ivar,並且可以在'updateHeight:'方法中直接更新它的值。 – Gandalf

回答

8

您可以使用contentInset補償29px額外的下邊距:

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