swift 加载网页 添加进度条

    /// 进度条:只在活动页展示
    fileprivate lazy var progressView:UIProgressView = UIProgressView()
    
    /// 进度条高度
    private var progressViewHeight:NSLayoutConstraint?


//设置监听
  webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)

//设置进度条高度约束
        progressViewHeight = progressView.heightAnchor.constraint(equalToConstant: 2)
        progressViewHeight?.isActive = true

            if #available(iOS 11.0, *) {
                progressView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
            } else {
                progressView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
            }




extension JYWebViewController: WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler {

    ///监听进度
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if (keyPath == "estimatedProgress") {
            progressView.progress = Float(self.webView.estimatedProgress)
        }
    }
    
    ///完成
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.webLoadSuccess()
        progressViewHeight?.constant = 0
    }
}

猜你喜欢

转载自www.cnblogs.com/qingzZ/p/11091265.html