Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UIWebView content's Height

I have a uiwebview that loads data using the loadHtmlString function.The thing is i am loading the data from an sqlite database, each time i load a different string with different length and naturally the web view obtains different height.
Now i need to know each time the exact height of the uiwebview in order to load an imageView right under the webView. I have tried

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;

    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"Size: %f, %f", fittingSize.width, fittingSize.height);
}

but i always get the same result. I need to know how can i get dynamically the height of web view each time it is loaded. Thx

like image 901
Elias Rahme Avatar asked Oct 29 '12 13:10

Elias Rahme


2 Answers

Try this

NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

int height = [result integerValue];

in your webViewDidFinishLoad method.

like image 142
Sumit Mundra Avatar answered Nov 02 '22 00:11

Sumit Mundra


You can get the height of the content in a web view by accessing the web view's scrollView property:

NSLog(@"%f",myWebView.scrollView.contentSize.height);
like image 29
Mick MacCallum Avatar answered Nov 01 '22 23:11

Mick MacCallum