使用Javascript获取webView内容的总高度 [英] Get total height of webView's content using Javascript

查看:1526
本文介绍了使用Javascript获取webView内容的总高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UIWebView 以HTML字符串的形式显示内容 - 而不是网站,高于iPhone的屏幕,而无需滚动在webView本身,将其留给父scrollView。

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.

为了实现这一点,我需要一种方法来获取总文档大小,包括可滚动区域,以设置webView的高度。我尝试了许多不同的Javascript解决方案:

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?

当前(非工作)代码:

[[[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;


推荐答案

在iOS 5.0中无需使用Javascript up - 你有直接的文档访问它的scrollView:

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;
    // ....
}

获取总高度webView的内容。

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

这篇关于使用Javascript获取webView内容的总高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆