使用'document.body.scrollHeight;'计算UIWebView内容的动态高度.在iOS 10中返回更大的价值 [英] Calculate UIWebView Content dynamic Height using 'document.body.scrollHeight;' returns bigger value in iOS 10

查看:91
本文介绍了使用'document.body.scrollHeight;'计算UIWebView内容的动态高度.在iOS 10中返回更大的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Objective C在应用程序上创建,我在其中使用 UIWebView 以HTML格式显示内容.我在 UIWebView 委托方法 webViewDidFinishLoad

I am creating on an application using Objective C, where I'm using a UIWebView to display contents in HTML format. I am using below code in UIWebView delegate method webViewDidFinishLoad

NSUInteger contentHeight = [[aWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollHeight;"]] intValue];

NSUInteger contentHeight = [[aWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollHeight;"]] intValue];

计算webview内容的高度,这在 iOS8 iOS9 iOS11 中工作正常,但在 iOS10 中运行正常> iPhone移动的内容高度返回的值比实际内容值大得多.因此,我在屏幕上的Webview底部获得了一些额外的空白.

to calculate the webview content height, this is working fine in iOS8, iOS9 and iOS11 but in iOS10 iPhone mobiles the content height returning a much bigger value than the actual content value. Because of this, I am getting some extra white space in bottom of my webview in screen.

我尝试了所有解决方案,但仅在iOS 10中得到了相同的错误内容高度.请帮助我解决此问题.预先谢谢您!

I tried all the solutions but getting the same wrong content height only in iOS 10. Please help me to resolve this problem. Thank You in Advance!

推荐答案

我使用以下方法获取contentSize.

I had used following way to get contentSize.

我在Webview的滚动视图中添加了观察者(KVO).

i have added observer(KVO) on webview's scrollview.

self.webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil);

现在,每当改变Webview的contentsize时,我都会通过这种方法获得回调

Now, whenever contentsize of webview will get changed, i get callback in this method

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
}

现在从此方法中获取对象的contentSize并使用它.

Now take the contentSize of object from this method and use that.

这篇关于使用'document.body.scrollHeight;'计算UIWebView内容的动态高度.在iOS 10中返回更大的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆