Is it possible to obtain a dynamic table view section header height using Auto Layout?

后端 未结 8 820
清歌不尽
清歌不尽 2020-11-28 02:36

New in iOS 8, you can obtain 100% dynamic table view cells by simply setting the estimated row height, then layout your elements in the cell using Auto Layout. If the conten

8条回答
  •  醉话见心
    2020-11-28 03:25

    Got stuck in the same issue where header was getting zero height untill and unless I provide a fixed height in the delegate for heighForHeaderInSection.

    Tried a lot of solutions which includes

    self.tableView.sectionHeaderHeight = UITableView.automaticDimension
    self.tableView.estimatedSectionHeaderHeight = 73
    

    But nothing worked. My cell were using proper autolayouts too. Rows were changing their height dynamically by using the following code but section header weren't.

    self.tableView.estimatedRowHeight = 135
    self.tableView.rowHeight = UITableView.automaticDimension
    

    The fix is extremely simple and weird too but I had to implement the delegate methods instead of 1 line code for the estimatedSectionHeaderHeight and sectionHeaderHeight which goes as follows for my case.

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        return 73
    }
    

提交回复
热议问题