Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  36] [ 6]  / answers: 1 / hits: 25229  / 14 Years ago, sat, june 5, 2010, 12:00:00

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;

More From » iphone

 Answers
8

There is no need to use Javascript in iOS 5.0 and up - you have direct, documented access to its scrollView:



- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
// ....
}


To get the total height of the contents of the webView.


[#96580] Wednesday, June 2, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazmyne

Total Points: 503
Total Questions: 102
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;