-3

created a webview which loads html text.

var html_string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

self.webView.loadHTMLString(html_string, baseURL: nil)

i want set webview height to its content.without scrolling in the webView

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • Possible duplicate of [UIWebView dynamic content size](https://stackoverflow.com/questions/27850792/uiwebview-dynamic-content-size) – rodskagg Feb 27 '19 at 11:22

2 Answers2

0

Mind that the component is WKWebView, Webview is a thing of the past.

Try this function:

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
}

Remember to create an outlet for myWebViewHeightConstraint and updateConstrain as well.

You have a nice day!

Helen Wood
  • 1,872
  • 2
  • 26
  • 41
0

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
}
AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • thanks for your reply, but i am not getting full content height, i have mentioned webView height 200 in storyboard. if the content height more than 200 it not increasing height but giving scrollable view – n shiva prasad reddy Feb 27 '19 at 13:25
  • Hi @nshivaprasadreddy i think you have set fixed height 200 from storyboard, you need to update this, set height 0 or greater then constraint (height >= 0) – AtulParmar Feb 27 '19 at 13:40
  • another option don't assign constraint just through auto resize – AtulParmar Feb 27 '19 at 13:42