Setting tableHeaderView height dynamically

后端 未结 8 811
梦如初夏
梦如初夏 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:16

    More condensed version of OP's answer, with the benefit of allowing layout to happen naturally (note this solution uses viewWillLayoutSubviews):

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    
        if let header = tableView.tableHeaderView {
            let newSize = header.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
            header.frame.size.height = newSize.height
        }
    }
    

    Thanks to TravMatth for the original answer.

提交回复
热议问题