I'm using a UIWebView
for displaying content, in the form of an HTML string – not a website, higher than the screen of the iPhone, without needing to scroll in the webView itself, leaving that to the parent scrollView.
To achieve this, I need a way to get the total document size, including the scrollable area, to set the webView's height. I have tried a number of different Javascript solutions:
(document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
document.body.offsetHeight // Returns zero
document.body.clientHeight // Returns zero
document.documentElement.clientHeight // Returns height of UIWebView
window.innerHeight // Returns height of UIWebView -2
document.body.scrollHeight // Returns zero
Is there a solution that actually works?
Current (nonworking) code:
[[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue];
NSLog(@"Content_height: %d", content_height);
CGRect rect = self.singlePost.contentText.frame;
rect.size.height = content_height;
self.singlePost.contentText.frame = rect;
When I tried with my code, I found,
(1)
return the height less than the original height. But(2)
return the actual height. My suggestion is to get theMax
of the two.Output
There is no need to use Javascript in iOS 5.0 and up - you have direct, documented access to its scrollView:
To get the total height of the contents of the webView.
I went with a slightly different approach, to create a <DIV> that I could key off of to get the size, because I was having no luck with inspecting document/body.
This is MonoTouch C#, but should work fine in Objective-C:
and getting the height in LoadFinished:
@Jesse, this works when updating with HTML that would generate a smaller size than was previously shown.
Where do you call your code? For me it returns 0 if it is called right after the loadHTMLString Method. If I call it in the (void)webViewDidFinishLoad:(UIWebView *)theWebView delegate, I get a valid value.