我有UITableView
,我正在添加从xib加载的tableFooterView
。
var footer = NSBundle.mainBundle().loadNibNamed("AFooterView", owner: self, options: nil).first as! AFooterView
self.tableView.tableFooterView = footer
这很好用,但我需要能够设置这个页脚的高度。 xib只有UIImageView
垂直和水平居中,因此无论视图的高度如何,它都会适应。
我不清楚如何使用AutoLayout
执行此操作?什么是正确的道路?
答案 0 :(得分:10)
对不起,我感到很抱歉,这里有更新后的答案:
我知道你可以通过设置框架高度来做到这一点,但它也可以通过在imageView加载完成后重新分配页脚视图来使用自动布局。
// either let auto layout calculate the frame, or set the frame yourself
// I set the width to an arbitrary size but it doesn't seem to matter,
// it will automatically be adjusted by the tableview when you assign it
CGFloat width = 100;
CGFloat height = 500;
footerView.frame = CGRectMake(0, 0, width, height);
// this is the "trick": re-assign the footerView after its size has been updated
// so that the tableView will show it correctly
tableView.tableFooterView = footerView;
有关详细信息,请参阅Resizing a UITableView’s tableHeaderView
最初的答案是关于章节页脚,而不是表格页脚
答案 1 :(得分:0)
如果您使用Autolayout
并从Header
加载Footer
或View
xib
,则应添加constraints
:
func setupTableFooterView() {
// Create your footer view from XIB and set constraints (in my case it is historyToolBarView of class HistoryToolBarView)
let view = Bundle.main.loadNibNamed("HistoryToolBarView", owner: self, options: nil)?.first
historyToolBarView = view as! HistoryToolBarView
historyToolBarView.translatesAutoresizingMaskIntoConstraints = false
historyToolBarView.addConstraints(
[NSLayoutConstraint.init(item: self.historyToolBarView,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 60),
NSLayoutConstraint.init(item: self.historyToolBarView,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: UIScreen.main.bounds.size.width)])
// Create a container of your footer view called footerView and set it as a tableFooterView
let footerView = UIView(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 60))
footerView.backgroundColor = UIColor.green
tableView.tableFooterView = footerView
// Add your footer view to the container
footerView.addSubview(historyToolBarView)
}