Created
April 5, 2017 18:33
-
-
Save johndelong/49b570a6b83a9b7565aabca2f8aefa10 to your computer and use it in GitHub Desktop.
Fitting Views to Their Content (edges cases)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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