0

I have used following methods but still now not getting the correct value:

[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]

[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]

webView.scrollView.contentSize.height

I have accessed all above in,

- (void)webViewDidFinishLoad:(UIWebView *)aWebView 
{

}
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Rooban Ponraj A
  • 281
  • 4
  • 20
  • see [this](http://stackoverflow.com/questions/745160/how-to-determine-uiwebview-height-based-on-content-within-a-variable-height-uit) and [this too](http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview) – DD_ Mar 05 '13 at 09:15

4 Answers4

2

You can get this by 2 ways

method 1: Load your html like this

embedHTML = [embedHTML stringByAppendingFormat:
               @"<body><div id='size_div'>%@</div></body></html>",yourString];

And now in webViewDidFinishLoad method keep this statements...

NSString *output = [webView stringByEvaluatingJavaScriptFromString:
                      @"document.getElementById(\"size_div\").offsetHeight;"];  
NSLog(@"height: %@", output);

Method 2:

UIWebView *objWebView = [[UIWebView alloc] init];
for (UIScrollView *v in objWebView.subviews) {
    if ([v isKindOfClass:[UIScrollView class]])
    {
        CGSize requiredSize = v.contentSize;
    }
}
Daniel
  • 20,420
  • 10
  • 92
  • 149
Murali
  • 1,869
  • 1
  • 14
  • 22
0

I think webView.scrollView.contentSize is set automatically by the webView itself when loading the HTML. Have you tried it exactly like this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     float h = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];

    // h is the height of the loaded webpage...

}

And don't forget to set the webView's delegate YourViewController: UIViewController <UIWebViewDelegate>

uruk
  • 1,094
  • 2
  • 14
  • 26
0

You can get webview height by using

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];
}
VRN
  • 13
  • 3
-1

use this

let height = webView.scrollView.contetSize.height
Berlin Raj
  • 124
  • 6