Question

Context

  • Single ViewController
  • Two UIWebView objects as subviews of a UIScrollView are in this ViewControllers view
  • The ViewController is the delegate of both UIWebViews
  • The delegate method - (void)webViewDidFinishLoad:(UIWebView *)webView gets called twice as expected for both UIWebViews
  • I'm setting the height of these UIWebViews using sizeThatFits once their content has loaded (in the webViewDidFinishLoad method

What I want to do

Once both WebViews have loaded their content I want to set the height of the UIScrollView that they are in relative to the WebViews height. Basically I want the UIScrollView to be tall enough to scroll all the way through the text in the UIWebViews.

Possible Solutions I've thought of

  • Have a counter within the webViewDidFinishLoad method, and when it is equal to the amount of WebViews on the view call a method that sets the height of the UIScrollView.
  • Call set height of UIScrollView in webViewDidFinishLoad - it will be called multiple times, but the last call will be the correct height.

Question

How do I work out when both webViews have loaded in a "better" way than the possible solutions above?

Was it helpful?

Solution

I think you're making this too complicated. As you suggest at the end of your question, why not just set the height (contentSize.height, I believe is what you want) with each webViewDidFinishLoad:? You can test to see which webview is the tallest and set the height to that value:

CGSize size = CGSizeMake(320, 0);
size.height = webView1.frame.size.height + webView2.frame.size.height;
scrollView.contentSize = size;

IMO it's not a great idea to be placing multiple webviews inside a scrollview (although I've done it before too...) Could you consider getting rid of the scroll view altogether and having a single webview (which is self-scrolling) and has two iframe elements, which each display your original webview1/webview2 content?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top