iOS - Swift中精准计算WKWebView的Conten

2023-04-02  本文已影响0人  俺不是大佬儿
let web_contentSizeKeyPath = "contentSize"
webView.scrollView.addObserver(self, forKeyPath: web_contentSizeKeyPath, options: .new,context:nil)

需要注意:webview加载中虽然isLoading = false链接加载完成,但是页面中的图片、视频等资源可能并未加载完成,此时的内容高度是一直在变化的回调的block会多次触发,返回的高度并不是最终的内容高度,最后一次返回的高度为精确的内容高度

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == web_contentSizeKeyPath {
            
            if self.infoWebView.estimatedProgress == 1.0 && self.infoWebView.isLoading == false  {//判断WebView加载完成
                
                infoWebView.evaluateJavaScript("document.readyState") { [weak self] (readyState, readyStateError) in
                    guard let self = self else {return}
                    
                    if let readyState = readyState as? String {
                        
                        if readyState == "complete"{//加载完成了
                            
                            self.infoWebView.evaluateJavaScript("document.body.scrollWidth",completionHandler: { [weak self](data, error ) in
                                guard let self = self else {return}
                                
                                //                let ratio = CGRectGetWidth(self.infoWebView.frame) / (data as? CGFloat ?? 0.0)
                                if let data = data as? CGFloat, data > 0 {
                                    //计算缩放比
                                    let ratio = infoWebView.frame.width / data
                                    //offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度 
                                    infoWebView.evaluateJavaScript("document.body.offsetHeight",completionHandler: { [weak self](data, error ) in
                                        guard let self = self else {return}
                                        
                                        //获取的高度不准确
                                        //let height = self.infoWebView.scrollView.contentSize.height
                                        
                                        var height:CGFloat = data as? CGFloat ?? 0.0
                                        height = height * ratio
                                        
                                        if self.webContentBgLayout_height.constant != height {
                                            
                                            self.webContentBgLayout_height.constant = height
                                            //高度回调
                                            if self.webContentHeightBlock != nil {
                                                self.webContentHeightBlock!(height)
                                                print("#####高度:%@",height)
                                            }
                                        }
                                    })
                                }
                            })
                        }
                    }
                }
            }
      }
}
上一篇下一篇

猜你喜欢

热点阅读