/ iOS

TableHeaderView 使用 AutoLayout 自适应高度

在 viewDidLayoutSubViews() 中设定 headerView 的高度

 override func viewDidLoad() {
     self.headerView = (Bundle.main.loadNibNamed("CreateOrderTableHeaderView", owner: self, options: nil)![0] as? CreateOrderTableHeaderView)
        
 }
 
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    sizeHeaderToFit()
}
 
func sizeHeaderToFit() {
    if let headerView = tableView.tableHeaderView {
 
        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()
 
        let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        var frame = headerView.frame
        frame.size.height = height
        headerView.frame = frame
 
        tableView.tableHeaderView = headerView
    }
}

高度还是有问题

在 ViewController 中尝试设置

    self.automaticallyAdjustsScrollViewInsets = false
    self.edgesForExtendedLayout = []