Smith
Smith

Reputation: 379

How to get proper scroll content size of UIWebView?

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?

Upvotes: 1

Views: 3242

Answers (3)

joern
joern

Reputation: 27620

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/

Upvotes: 0

sage444
sage444

Reputation: 5684

also try use offsetWidth/offsetHeight

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

Upvotes: 1

cutsoy
cutsoy

Reputation: 10251

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.

Upvotes: 1

Related Questions