我如何获得一个UITableView tableFooterView来扩展以填充整个父视图?
问题描述:
我有一个UIView,我设置为UITableView tableFooterView属性的属性。在表格视图和页脚不填充整个父视图的情况下,是否有办法确定页脚需要多高才能填充剩余空间?我如何获得一个UITableView tableFooterView来扩展以填充整个父视图?
这里我的最终目标是让删除按钮与视图底部对齐。如果表视图大于父视图,那么我不会执行任何操作,并且删除按钮将从视图中启动,这很好。
编辑这需要在表单工作表模式类型的iPad上工作,其中视图范围应该只是表单表单的范围,而不是整个屏幕。
答
关闭我的头顶:因为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);
}
答
只有一个想法:如果表视图具有固定的行高度,则可以将行数乘以行高加上一些固定量。如果行高不固定,可以总结所有不同的高度。
答
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是表的底部和它的容器视图的底部之间的距离。
答
@ 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()
}