TableHeaderView、TableFooterView 自动高度
UIView 中的设置
import UIKit
class MultiLineLabelHeaderFooterView: UIView {
@IBOutlet weak var contentLabel: UILabel!
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override func awakeFromNib() {
super.awakeFromNib()
self.contentLabel.textColor = UIColor.textMinorColor
}
override func layoutSubviews() {
super.layoutSubviews()
contentLabel.preferredMaxLayoutWidth = contentLabel.bounds.width
}
}
UIViewController 中的设置
设置 初始化 footerView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// ... 其他设置
let footerView = Bundle.loadView(fromNib: "MultiLineLabelHeaderFooterView", withType: MultiLineLabelHeaderFooterView.self)
footerView.contentLabel.text = "这里是 tableFooterView 这里是 tableFooterView 这里是 tableFooterView 这里是 tableFooterView 这里是 tableFooterView 这里是 tableFooterView 这里是 tableFooterView 这里是 tableFooterView "
self.tableView.tableFooterView = footerView
self.queryPlanList()
}
autoLayout 重排
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let footerView = tableView.tableFooterView {
let height = footerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var frame = footerView.frame
if height != frame.size.height {
frame.size.height = height
footerView.frame = frame
self.tableView.tableFooterView = footerView
}
}
}