swift WKWebview獲取內(nèi)容高度自適應(yīng)


import UIKit
import WebKit

class MLWebViewHeightViewController: MLBaseViewController {
    
    var urlString: String?
    @IBOutlet weak var testScrollView: UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.urlString = "https://news.cnblogs.com/n/588790/"
        self.buildUI()
    }
    
    func buildUI() {
        self.testScrollView.addSubview(self.changeWebView)
        self.testScrollView.addSubview(self.testLabel)
        
        self.testLabel.snp.makeConstraints { [unowned self] (make) in
            make.width.equalToSuperview()
            make.height.equalTo(60.0)
            make.top.equalTo(self.changeWebView.snp.bottom)
        }
        
        self.testScrollView.sizeToFit()
        
        if let webUrlString = self.urlString {
            if let encodedStr = webUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
                if let myUrl = URL(string: encodedStr) {
                    let myRequest = URLRequest(url: myUrl)
                    self.changeWebView.load(myRequest)
                }
            }
        }
    }
    
    // MARK: - 懶加載
    
    lazy var changeWebView: WKWebView = {
        let webConfiguration = WKWebViewConfiguration()
        //初始化偏好設(shè)置屬性:preferences
        webConfiguration.preferences = WKPreferences()
        //是否支持JavaScript
        webConfiguration.preferences.javaScriptEnabled = true
        //不通過用戶交互,是否可以打開窗口
        webConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = false
        
        let webFrame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0)
        let webView = WKWebView(frame: webFrame, configuration: webConfiguration)
        webView.backgroundColor = UIColor.blue
        webView.navigationDelegate = self
        webView.scrollView.isScrollEnabled = false
        webView.scrollView.bounces = false
        webView.scrollView.showsVerticalScrollIndicator = false
        webView.scrollView.showsHorizontalScrollIndicator = false
        
        return webView
    }()
    
    lazy var testLabel: UILabel = {
        let tempLabel = UILabel()
        tempLabel.text = "我是一個(gè)Label"
        tempLabel.textAlignment = .center
        tempLabel.backgroundColor = UIColor.red
        
        return tempLabel
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension MLWebViewHeightViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        var webheight = 0.0
        
        // 獲取內(nèi)容實(shí)際高度
        self.changeWebView .evaluateJavaScript("document.body.scrollHeight") { [unowned self] (result, error) in
            
            if let tempHeight: Double = result as? Double {
                webheight = tempHeight
                print("webheight: \(webheight)")
            }
            
            DispatchQueue.main.async { [unowned self] in
                var tempFrame: CGRect = self.changeWebView.frame
                tempFrame.size.height = CGFloat(webheight)
                self.changeWebView.frame = tempFrame
                self.testScrollView.contentSize = CGSize(width: self.testScrollView.frame.size.width, height: self.changeWebView.frame.size.height + self.testLabel.frame.size.height)
            }
        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容