1

I'm trying to get the UIWebView scroll content size inside - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType method so as to show an arrow image if horizontal scroll exists.

1) I have tried webView.scrollview.contentSize to get the scroll content size but it is always giving me the webview frame values even if content size is more.

2) I have tried

CGSize contentSize = CGSizeMake([[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] floatValue],
                                [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]);

Even the above 2nd approach is not giving the proper content size.

Is their a straight forward or screwed way of getting accurate webview's embedded scrollView's content size?

Smith
  • 379
  • 6
  • 18

3 Answers3

1

This isn't the answer you were hoping for I guess but you're approach is wrong.

- (BOOL)webView:(UIWebView *)webView should**Start**LoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

You see, when this method is called, it's called because a new request is going to be started. At that point, the UIWebView has yet to receive your page and thus cannot possibly give you a correct contentSize.

cutsoy
  • 10,127
  • 4
  • 40
  • 57
1

also try use offsetWidth/offsetHeight

CGSize contentSize = CGSizeMake([[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetWidth;"] floatValue],
                                [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]);
sage444
  • 5,661
  • 4
  • 33
  • 60
  • This works for me, with the exception that I have to add about 20 puts to the height to get it working correctly. (The height of main concern to me in my case). Doesn't appear to be scroll view content inset, as these seem to be 0. – Chris Prince Jan 09 '15 at 18:59
  • Be careful! I just discovered a correlation between zoom scale. When I _request_ the size using JS, `[scrollView setZoomScale:scale animated:YES];` has no effect anymore, no matter the call order. – Julian F. Weinert Jul 23 '16 at 13:43
0

Check out this blogpost that I wrote a while ago when I had to do something similar: http://www.pixeldock.com/blog/set-the-height-of-a-uiwebview-to-the-height-of-its-html-content/

joern
  • 27,354
  • 7
  • 90
  • 105