0

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?

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
drc
  • 1,905
  • 4
  • 22
  • 28
  • I dont know if this is the best but i would add 2 booleans globaly, and when the delegate method is called it checks what tag the webView has and set the booleans, and then add an if statement to chekc if both booleans are true. – Arbitur Aug 01 '13 at 19:04

1 Answers1

0

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?

TomSwift
  • 39,369
  • 12
  • 121
  • 149
  • I'm using webViews to display text returned from an API - it has to be webViews as the API may contain HTML, but I want to have native elements around this text, hence the need for webviews inside a scroll view. – drc Aug 01 '13 at 19:34
  • I might end up using a 3rd part library to convert the HTML into attributed strings. But right now exploring UIWebView. – drc Aug 01 '13 at 19:39
  • Also your interpretation or my wording of question is a little off. I want the scroll view to be taller than the heights of the webviews combined! – drc Aug 01 '13 at 20:04
  • can you edit (so the code sample adds the combined heights of the webviews) you answer so I can accept it as correct. – drc Aug 02 '13 at 18:44