2
votes

I want to display HTML content inside on my view. Therefore I am rendering the HTML content using webview. The webview is inside a uiscrollview.

Because the HTML changes I need to adjust the height of the webview and the scrollview. I do it this way:

func webViewDidFinishLoad(webView: UIWebView) {

    webView.scrollView.scrollEnabled = false
    webView.frame.size.height = webView.scrollView.contentSize.height

    self.scrollView.contentSize.height = 267.0 + webView.scrollView.contentSize.height


}

But when I running the app this is what happens:

At first it looks great. But when I start scrolling, the webview shrinks to its original size:

enter image description here

Any idea why the webview is shrinking when I start scrolling the scrollview?

I thought that maybe it has something to with constraints. Maybe it tries to revert the height of the webview to it's original height.

1
This might be a HTML issue. Have you checked how many times webViewDidFinishLoad gets called and how webview content height is changed ?Zell B.
No, How can I check that?Oded Harth
just add a println(webView.scrollView.contentSize.height) inside method and check the output in xcodeZell B.
But it only changes after I start scrolling, so I need a way to trigger this only after I start scrolling.Oded Harth

1 Answers

3
votes

Looks like it was a constraint problem, because when I add to the webViewDidFinishLoad this line:

    webView.addConstraint(NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute(rawValue: 8)!, relatedBy: NSLayoutRelation(rawValue: 0)!, toItem: nil, attribute: NSLayoutAttribute(rawValue: 0)!, multiplier: 1.0, constant: (webView.scrollView.contentSize.height + 600.0)))

It fixed the problem.