javascript - 在 iOS 10 中获取网页 View 的高度

标签 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 有这个问题

最佳答案

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

您可以使用 javascript func scrollHeight 来查找您需要的内容。

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

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

CGFloat webViewHeight = [heightStr floatValue];

关于javascript - 在 iOS 10 中获取网页 View 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510790/

相关文章:

javascript - AddEventListener 不工作 ES6

javascript - 旧版本节点中来自数据库的动态路由

ios - 从 App Store 下载应用程序时崩溃,但在测试时不会崩溃

ios - 如何在 AFNetworking 的 AFHTTPSessionManager 中超时或检测慢速响应?

javascript - jQuery 检查给定 div 中每个输入的值

javascript - 如何从上传表单中获取图像的来源?

ios - 向 UILabel 问题添加高度约束

ios - 重新排序 UIImage 数组中的元素

ios - 以全分辨率从 UIScrollView 中捕获缩放的 UIImageView

objective-c - 非常奇怪的 ARC 内存泄漏