2011-06-01 30 views
9

我有一個UIView,我設置爲UITableView tableFooterView屬性的屬性。在表格視圖和頁腳不填充整個父視圖的情況下,是否有辦法確定頁腳需要多高才能填充剩餘空間?我如何獲得一個UITableView tableFooterView來擴展以填充整個父視圖?

這裏我的最終目標是讓刪除按鈕與視圖底部對齊。如果表視圖大於父視圖,那麼我不會執行任何操作,並且刪除按鈕將從視圖中啓動,這很好。

編輯這需要在表單工作表模式類型的iPad上工作,其中視圖範圍應該只是表單表單的範圍,而不是整個屏幕。

回答

14

關閉我的頭頂:因爲UITableViews本質上是UIScrollViews,請嘗試使用表視圖的contentSize.height值來查看佔用屏幕的多少。然後,調整tableFooterView框架以填充其超級視圖框架高度的高度差。基本上:

CGRect tvFrame = tableView.frame; 
CGFloat height = tvFrame.size.height - tableView.contentSize.height; 
if (height > MIN_HEIGHT) { // MIN_HEIGHT is your minimum tableViewFooter height 
    CGRect frame = tableFooterView.frame; 
    tableFooterView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height); 
} 
0

只有一個想法:如果表視圖具有固定的行高度,則可以將行數乘以行高加上一些固定量。如果行高不固定,可以總結所有不同的高度。

0
CGRect parentFrame = myParentView.frame; //tells you the parents rectangle. 
    CGRect tableFrame = myTableView.frame; // tells you the tableView's frame relative to the parent. 

    float delta = parentFrame.size.height - tableFrame.size.height - tableFrame.origin.y; 

delta是表的底部和它的容器視圖的底部之間的距離。

3

@ octy的答案將適用於iOS 9.但是,對於iOS 10,似乎tableView的contentSize包含tableViewFooter高度。在iOS 10中,我不得不做以下事情:

var rowDataBounds: CGRect { 
    get { 
     if numberOfSections <= 0 { 
      return CGRect(x: 0, y: 0, width: frame.width, height: 0) 
     } 
     else { 
      let minRect = rect(forSection: 0) 
      let maxRect = rect(forSection: numberOfSections-1) 
      return maxRect.union(minRect) 
     } 
    } 
} 

fileprivate func resizeFooterView(){ 

    if let footerView = tableFooterView { 

     var newHeight: CGFloat = 0 
     let tvFrame = self.frame; 

     if #available(iOS 10, *) { 

      newHeight = tvFrame.size.height - rowDataBounds.height - self.contentInset.bottom - self.contentInset.top 

     } 
     else { 

      newHeight = tvFrame.size.height - self.contentSize.height 

     } 
     if newHeight < 0 { 
      newHeight = 0 
     } 
     let frame = footerView.frame 
     if newHeight != frame.height { 
      footerView.frame = CGRect(x:frame.origin.x, y:frame.origin.y, width:frame.size.width, height: newHeight) 
     } 
    } 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 
    resizeFooterView() 
} 
相關問題