Setting tableHeaderView height dynamically

后端 未结 8 812
梦如初夏
梦如初夏 2020-12-01 04:16

My application creates a UITableViewController that contains a custom tableHeaderView which may have an arbitrary height. I\'ve been struggling with a way to set this header

8条回答
  •  既然无缘
    2020-12-01 05:06

    Based on @TravMatth and @NSExceptional's answer:

    For Dynamic TableView Header, with multiple line of text(No matter have or not)

    My solution is:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        if let footView = tableView.tableFooterView {
            let newSize = footView.systemLayoutSizeFitting(CGSize(width: self.view.bounds.width, height: 0))
            if newSize.height != footView.frame.size.height {
                footView.frame.size.height = newSize.height
                tableView.tableFooterView = footView
            }
        }
    }
    

    tableView.tableFooterView = footView to make sure that your tableview Header or Footer updated. And if newSize.height != footView.frame.size.height helps you not to be called this method many times

提交回复
热议问题