[英]How can I get a UITableView tableFooterView to expand to fill the whole parent view?
我有一個 UIView ,我將其設置為 UITableView tableFooterView 屬性的屬性。 如果表格視圖和頁腳沒有填滿整個父視圖,有沒有辦法確定我需要多高才能使頁腳填充剩余空間?
我的最終目標是讓刪除按鈕與視圖底部對齊。 如果表視圖大於父視圖,我不會做任何事情,刪除按鈕將從視圖中消失,這很好。
編輯這需要在表單模式類型中的 iPad 上工作,其中視圖邊界應該只是表單的邊界,而不是整個屏幕。
突然想到:由於 UITableViews 本質上是 UIScrollViews,因此請嘗試使用 table view 的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);
}
@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()
}
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 是表格底部與其容器視圖底部之間的距離。
一個想法:如果表格視圖具有固定的行高,您可以將行數乘以行高加上一些固定的數量。 如果行高不固定,您可以總結所有不同的高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.