在iOS 10中获取Web视图的高度

时间:2016-09-15 12:15:01

标签: javascript ios objective-c uiwebview

我需要获得UIWebView高度。在方法webViewDidFinishLoad我有

CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] floatValue];
CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] floatValue];
CGFloat height3 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.clientHeight"] floatValue];
CGFloat height4 = [[webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.clientHeight"] floatValue];
CGFloat height5 = [[webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.scrollHeight"] floatValue];
CGFloat height6 = [[webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.offsetHeight"] floatValue];

height = MAX(height1, MAX(height2, MAX(height3, MAX(height4, MAX(height5, height6)))));

仍然所有值都小于实际值 我只在iOS 10中遇到此问题

1 个答案:

答案 0 :(得分:13)

从Apple论坛,您的问题似乎有解决方案

您可以使用javascript func scrollHeight查找所需内容。

NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

您只需要将其转换为可用的浮点值:

CGFloat webViewHeight = [heightStr floatValue];
相关问题