Kex
Kex

Reputation: 8629

How do I get the height of a dynamically sized section header in UITableView?

I have a UITableView, it uses a custom UIView as the section header view. Code like so:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ECStartTableHeaderView") as! ECStartTableHeaderView
  return headerView
}

I have UITableViewAutomaticDimension for the height on the header. I need to find out what this height is in my code. How can I get this header height?

Upvotes: 1

Views: 1014

Answers (2)

Ajay saini
Ajay saini

Reputation: 2470

You can take out the Height of the Header View Just Like.

if let headerView = tableView.tableHeaderView {
   height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
}

Upvotes: 0

axunic
axunic

Reputation: 2316

I think you have to ensure your header layout finished first, so you can get actual height.

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let height = view.frame.height
}

Upvotes: 1

Related Questions