【问题标题】:Get the height of a web view in iOS 10 [closed]在 iOS 10 中获取 Web 视图的高度 [关闭]
【发布时间】:2016-09-15 12:15:01
【问题描述】:

我需要获取 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 中遇到这个问题

【问题讨论】:

  • webView 高度还是 webView 内容高度?

标签: javascript ios objective-c uiwebview


【解决方案1】:

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

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

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

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

CGFloat webViewHeight = [heightStr floatValue];

【讨论】:

  • 这没有回答问题。 OP 将那个用于 height1 并得到了错误的值,我也是。
  • 它对我有用!我设法在 webViewDidFinishLoad 中获得了正确的值。我在早期版本的 iOS 中使用 @"document.height"。 @"document.body.scrollHeight;"就我而言,在 iOs10 中运行良好。
  • 它适用于旧版本,并在 8.1、9.3 和 10 上测试过
  • @Francesca 我遇到了 iOS 10 的问题 stackoverflow.com/questions/45994641/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多