How to determine UIWebView height based on content, within a variable height UITableView?

This code is probably too slow for table view use, but does the trick. It doesn't look like there's any alternative, as UIWebView offers no direct access to the DOM.

In a view controller's viewDidLoad I load some HTML in a webview and when the load is finished run some javascript to return the element height.

- (void)viewDidLoad
{
    [super viewDidLoad];
    webview.delegate = self;
    [webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
    NSLog(@"height: %@", output);
}

I was very glad to find your answers, the javascript trick worked well for me.

However, I wanted to say that there is also the "sizeThatFits:" method :

CGSize goodSize = [webView sizeThatFits:CGSizeMake(anyWidth,anyHeigth)];

We have to set a preferred size different from zero, but apart from that, it usually always works !

Usually, because with the UIWebViews, it seems to work only on the second loading (I use the "loadHTMLString:" method, and yes I call "sizeThatFits:" from the delegate "didFinishLoad:" method).

So, that's why I was very happy to find your solution which works in any case.


This seems to work. For now.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat webViewHeight = 0.0f;
    if (self.subviews.count > 0) {
        UIView *scrollerView = [self.subviews objectAtIndex:0];
        if (scrollerView.subviews.count > 0) {
            UIView *webDocView = scrollerView.subviews.lastObject;
            if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])
                webViewHeight = webDocView.frame.size.height;
        }
    }
}

It should safely return 0 if it fails.


The problem with -sizeThatFits: is that it doesn't work out of the box, at least not on iOS 4.1. It just returns the current size of the webView (no matter whether it's called with CGSizeZero or an arbitrary non-zero size).

I found out that it actually works if you reduce the frame height of the webView prior to calling -sizeThatFits:.

See my solution: How to determine the content size of a UIWebView?


The better approach is to use scrollView.contentSize.height property, like below.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
     NSLog(@"%f",webView.scrollView.contentSize.height);   
}