我使用了以下方法,但现在仍未获得正确的值:
[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]
[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]
webView.scrollView.contentSize.height
我已经访问了以上所有内容,
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
}
答案 0 :(得分:2)
你可以通过2种方式获得这个
方法1:像这样加载你的html
embedHTML = [embedHTML stringByAppendingFormat:
@"<body><div id='size_div'>%@</div></body></html>",yourString];
现在在webViewDidFinishLoad方法中保留此语句......
NSString *output = [webView stringByEvaluatingJavaScriptFromString:
@"document.getElementById(\"size_div\").offsetHeight;"];
NSLog(@"height: %@", output);
方法2:
UIWebView *objWebView = [[UIWebView alloc] init];
for (UIScrollView *v in objWebView.subviews) {
if ([v isKindOfClass:[UIScrollView class]])
{
CGSize requiredSize = v.contentSize;
}
}
答案 1 :(得分:0)
我认为加载HTML时,webView本身会自动设置webView.scrollView.contentSize
。你有没有尝试过这样:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
float h = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];
// h is the height of the loaded webpage...
}
不要忘记设置webView的委托YourViewController: UIViewController <UIWebViewDelegate>
答案 2 :(得分:0)
您可以使用
获取网页浏览量- (void)webViewDidFinishLoad:(UIWebView *)webView
{
float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];
}
答案 3 :(得分:-1)
使用此
let height = webView.scrollView.contetSize.height