Javascript 获取iOS 10中web视图的高度
我需要获取UIWebView高度。在我使用的webViewDidFinishLoad方法中Javascript 获取iOS 10中web视图的高度,javascript,ios,objective-c,uiwebview,Javascript,Ios,Objective C,Uiwebview,我需要获取UIWebView高度。在我使用的webViewDidFinishLoad方法中 CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] floatValue]; CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHe
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中才遇到这个问题在苹果论坛上,你的问题似乎有解决方案 您可以使用javascript函数scrollHeight来查找所需内容
NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];
您只需将其转换为可用的浮点值:
CGFloat webViewHeight = [heightStr floatValue];
webView高度或webView内容高度?这并不能回答问题。OP用那个值表示高度1,得到了一个错误的值,我也是。它对我有效!我设法在webViewDidFinishLoad中获得了正确的值。我在iOS的早期版本中使用了@“document.height”@“document.body.scrollHeight;”在我的例子中在iOs10中运行良好。它在使用8.1、9.3和10.0进行测试的旧版本中运行良好10@FrancescaI我在iOS 10上遇到了问题