Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate UIWebView Content dynamic Height using 'document.body.scrollHeight;' returns bigger value in iOS 10

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];

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.

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!

like image 422
Anand Gautam Avatar asked Sep 01 '17 06:09

Anand Gautam


1 Answers

I had used following way to get contentSize.

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

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

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?) {
}

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

like image 191
Mehul Thakkar Avatar answered Oct 08 '22 17:10

Mehul Thakkar