2016-09-15 25 views
3

我需要获得UIWebView的高度。在方法webViewDidFinishLoad我获取iOS 10网页视图的高度

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

+0

webView的高度或视图内容高度? –

回答

13

有这个问题,从苹果的论坛,您的问题似乎有了解决

你可以使用javascript func的scrollHeight来找到你需要的东西。

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

你只需要将其转换一个可用的浮点值:

CGFloat webViewHeight = [heightStr floatValue]; 
+0

这不回答这个问题。 OP使用那一个为height1并且得到了一个错误的值,我也是如此。 – Superwayne

+0

它为我工作!我设法在webViewDidFinishLoad中获得了正确的值。我在早期版本的iOS中使用@“document.height”。 @ “document.body.scrollHeight;”在我的情况下,在iOs10中运行良好。 –

+0

它的工作和旧版本的工作,与8.1,9.3和10测试 –