Skip to content

Instantly share code, notes, and snippets.

@johndelong
Created April 5, 2017 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndelong/49b570a6b83a9b7565aabca2f8aefa10 to your computer and use it in GitHub Desktop.
Save johndelong/49b570a6b83a9b7565aabca2f8aefa10 to your computer and use it in GitHub Desktop.
Fitting Views to Their Content (edges cases)
/**
Calculates and adjusts the frame of a web view to fit its content
http://stackoverflow.com/a/3937599/7066201
*/
func calculateWebViewSize() {
self.view.layoutIfNeeded()
var frame = self.webview.frame
frame.size.height = 1
self.webview.frame = frame
let fittingSize = self.webview.sizeThatFits(.zero)
frame.size = fittingSize
self.webview.frame = frame
self.webViewHeightConstraint.constant = frame.size.height
self.view.layoutIfNeeded()
}
/**
UITableView header/footer views (not to be confused with section header/footer views) do not use autolayout.
Therefore you have to manually adjust the `tableViewHeader`/`tableViewFooter` frame to be the size that fits
your custom view.
http://stackoverflow.com/a/29964310/7066201
*/
func sizeFooterToFit() {
guard let footer = self.tableView.tableFooterView else { return }
footer.setNeedsLayout()
footer.layoutIfNeeded()
let height = footer.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var frame = footer.frame
frame.size.height = height
footer.frame = frame
self.tableView.tableFooterView = footer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment