Skip to content

Instantly share code, notes, and snippets.

@fcaldarelli
Created January 16, 2020 01:53
Show Gist options
  • Save fcaldarelli/e0576ffe6434e9b4c4ed9a52a2557fe7 to your computer and use it in GitHub Desktop.
Save fcaldarelli/e0576ffe6434e9b4c4ed9a52a2557fe7 to your computer and use it in GitHub Desktop.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let headerView = tableView.tableHeaderView else {
return
}
// The table view header is created with the frame size set in
// the Storyboard. Calculate the new size and reset the header
// view to trigger the layout.
// Calculate the minimum height of the header view that allows
// the text label to fit its preferred width.
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
if headerView.frame.size.height != size.height {
headerView.frame.size.height = size.height
// Need to set the header view property of the table view
// to trigger the new layout. Be careful to only do this
// once when the height changes or we get stuck in a layout loop.
tableView.tableHeaderView = headerView
// Now that the table view header is sized correctly have
// the table view redo its layout so that the cells are
// correcly positioned for the new header size.
// This only seems to be necessary on iOS 9.
tableView.layoutIfNeeded()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment