简体   繁体   中英

WKWebView contentSize height not changing after resize font size

I use a UIScrollView and put a few UILabels and a WKWebView at the bottom. WkWebView frame size always equal to its contentSize and I disable its userInteraction. I use html string to display data in WKWebView . I can change the font size in WKWebView by replacing keyword in css before I load the html string into WKWebView.

My problem is when I increase the font size, WKWebView's scrollView.contentSize.height also increase but when I decrease the font size, WKWebView's scrollView.contentSize.height did not descrease. It always remember its largest value. This leave a large white space in WKWebView and my UIScrollView. How can I make WKWebView's scrollView.contentSize fit its content?

This is my code to resize UIScrollView when WKWebView finish load its content.

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print(webView.scrollView.contentSize.height)
    self.webView.frame = CGRect(x: self.webView.frame.origin.x, y: self.webView.frame.origin.y, width: webView.scrollView.contentSize.width, height: webView.scrollView.contentSize.height)
    self.scrollView.contentSize = CGSize(width: contentView.bounds.width, height: self.webView.frame.origin.y + webView.scrollView.contentSize.height)
}

I handled it by saving the height of initial font size (small font) in a variable and on change font size its returning the correct height for large font but not returning the correct height when return to small font again .... thats why i am not calculating the font after return to small font again but taking the value from variable that i save before ..

var webviewHeightForSmallFont : CGFloat = 0


webView.evaluateJavaScript("document.documentElement.scrollHeight", completionHandler: { (height, error) in
                                        let h = height as! CGFloat
                                        if self.textSize == 100 && self.webviewHeightForSmallFont == 0{
                                            self.webviewHeightForSmallFont = h
                                        }
                                        if self.webviewHeightForSmallFont != 0 && self.textSize == 100 {
                                            self.delegate!.webViewDidGotHeight(self.webviewHeightForSmallFont)
                                        }
                                        else if self.cellHeight != h {
                                            self.delegate!.webViewDidGotHeight(h)
                                        }
                                        else if h == 0 {
                                            self.delegate!.webViewDidFaildToGotHeight()
                                        } 

in the above code 100 size is for small font and 150 for large code ... and above part of code checking if font is set to small and don't have other value then 0 ... then save the height in webviewHeightForSmallFont variable and if its already have some value and font is small then use last previous value for that ... because its work fine for large.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM