Swift WKWebView侧滑返回时html逐级返回,而不是直接返回到上级控制器?

        self.webView.allowsBackForwardNavigationGestures = true

没错,就这

private let canGoBackKeyPath = "canGoBack"

webView.addObserver(self, forKeyPath: canGoBackKeyPath, options: .new, context: nil)

open override func observeValue(forKeyPath keyPath: String?,
                                    of object: Any?,
                                    change: [NSKeyValueChangeKey: Any]?,
                                    context: UnsafeMutableRawPointer?) {
        guard let theKeyPath = keyPath, object as? WKWebView == webView else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
            return
        }
        if theKeyPath == canGoBackKeyPath{
            if let newValue = change?[NSKeyValueChangeKey.newKey]{
                let newV = newValue as! Bool
                if newV == true{
                    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false;
                }else{
                    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true;

//这是为了处理vc的侧滑手势与webview 手势会有冲突
                }
            }
        }
    }


deinit {
        webView.removeObserver(self, forKeyPath: canGoBackKeyPath, context: nil)
    }

你可能感兴趣的:(Swift WKWebView侧滑返回时html逐级返回,而不是直接返回到上级控制器?)