WKWebView does not resize when content changes

WKWebView does not change when content changes.

Here is the code snippet:

@IBOutlet weak var containingView: UIView! var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() let contentController = WKUserContentController(); self.webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: WKWebViewConfiguration()) self.webView.autoresizingMask = [.FlexibleHeight] self.webView.loadHTMLString(__, baseURL: __)) self.webView.scrollView.bounces = false self.webView.scrollView.scrollEnabled = false self.containingView.addSubview(self.webView) } 

What function should I add so that I can intercept the size of the content that has been resized and resize the contained image accordingly?

+6
source share
2 answers

What stopped me was the small initial height of WKWebView

 webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, 150), configuration: configuration) 

instead

 webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: configuration) 
+2
source

Conceptually in lens c you have to do as shown below.

Two things: Make sure you have these properties:

 self.webView.scalesPageToFit = YES; self.webView.contentMode = UIViewContentModeScaleAspectFit; 

And finally, calculate the height of the contents of webView as: - webView.isLoading is one of the most important properties. In fact, in one call to the web url, it is called several times.

 - (void)webViewDidFinishLoad:(UIWebView *)webView{ if (webView.isLoading == NO)// as it is called multiple times so wait untill last call { CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue]; } } 

This is how you should calculate the height of the web view.

you can look here, since WKWebView has no deletion, which you can use to get the content size instead of KVO:

KVO Example Maybe this will help you more!

-3
source

All Articles