Although theoretically, the JavaScript method provided
- (void)webViewDidFinishLoad:(UIWebView *)webview CGRect oldBounds = [[self webview] bounds]; CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue]; [webview setBounds:CGRectMake(oldBounds.x, oldBounds.y, oldBounds.width, height)]; }
should work (and seems to work for most people), it's just not for me. I tried with several options and it always returned a size that has nothing to do with the true height of the content. For example, one liner will have a height of 260 or 244. I donβt know why (I would like to know).
SO, I ended up doing as hinted by this answer:
- (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); }
I think the JS style is more elegant, but hell, it does not work for me and cannot understand why, while this method worked out of the box.
source share