Use following code for this, I hope this helps you.
Note:- 'UIWebView' was deprecated in iOS 12.0: No longer supported, So please use WKWebView
instead of UIWebView
For UIWebView
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.scrollView.isScrollEnabled=false;
myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height
webView.scalesPageToFit = true
}
For WKWebView
If you want to use WKWebView then you need to do the following things.
1) import WebKit
2) make your ViewController inherit from WKNavigationDelegate
3) hook up the WKWebView’s delegate: webView.navigationDelegate = self
4) implement the following protocol function:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame.size.height = 1
webView.frame.size = webView.scrollView.contentSize
}