Skip to content

Instantly share code, notes, and snippets.

@hossamghareeb
Created February 24, 2019 14:58
Show Gist options
  • Save hossamghareeb/904a1f903fd970fec6d6fe20562940ac to your computer and use it in GitHub Desktop.
Save hossamghareeb/904a1f903fd970fec6d6fe20562940ac to your computer and use it in GitHub Desktop.
Dynamic UITable header view
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateTableViewHeaderHeight()
}
private func updateTableViewHeaderHeight() {
guard let headerView = tableView.tableHeaderView else { return }
/// Get the size that should meet the constraints
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
guard size.height != headerView.frame.height else { return }
headerView.frame.size.height = size.height
tableView.tableHeaderView = headerView
tableView.layoutIfNeeded() // make sure to proper layout cells for the new header size.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment