I need to resize the web view frame according to content height. So I need to calculate the height of content.
Asked
Active
Viewed 6,078 times
1
-
1Already solution has been provided a this [link]. [1]: http://stackoverflow.com/questions/16540050/uiwebview-resize-height-to-fit-content – Anand Apr 22 '14 at 08:52
-
1possible duplicate of [How to get the content height of a UIWebView in iPhone sdk, objective-c?](http://stackoverflow.com/questions/3837214/how-to-get-the-content-height-of-a-uiwebview-in-iphone-sdk-objective-c) – KIDdAe Apr 22 '14 at 08:52
2 Answers
5
This is the solution that i use
NSUInteger contentHeight = [[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollHeight;"]] intValue];
Your webview should have a frame smaller than all possible content size

Tekaya Marouene
- 620
- 3
- 14
-
Important to know: I had a lot of issues using this on a certain piece of html/css from a 3rd party. After searching I came to the conclusion that you can get into trouble when the html contains viewport-relative sizing in its CSS. The typical "make 0 pixels high, render webview, query height, set webview height to result" does not work, because if e.g. a font-size is defined as 3.4vh the text will change size when you increase the webview height, invalidating the result of your calculation. – Joris Mans Jan 30 '17 at 13:29
1
Try This,
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize contentSize = webView.scrollView.contentSize;
NSLog(@"Content Size %@", NSStringFromCGSize(contentSize));
}

NKB
- 651
- 5
- 13