WKWebview弹出窗口的PDF关闭按钮?



我的应用正在运行WKWebview以显示网站。当单击元素时,它会调用window.open()该元素被捕获,并根据链接显示PDF:

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
popupWebView = WKWebView(frame: view.bounds, configuration: configuration)
popupWebView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
popupWebView!.navigationDelegate = self
popupWebView!.uiDelegate = self
popupWebView!.allowsBackForwardNavigationGestures = true

view.addSubview(popupWebView!)
return popupWebView!
}

就我而言,它工作得很好。

我的问题是现在我无法返回,因为 PDF 是全屏打开的。是否可以在这种弹出式 Web 视图中启用导航控件?或者有没有办法通过"后退"滑动来控制背部行为?我试图通过allowsBackForwardNavigationGestures做到这一点,但它似乎不起作用......

我认为WKWebView没有简单的方法来添加后退按钮,您需要在视图(或导航控制器)上添加后退按钮。我已经能够这样做:

观察 webView 实例的canGoBack属性更改,以便启用/禁用(或隐藏/显示)后退按钮,如下所示:

popupWebView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoBack), options: .new, context: nil)

在视图控制器上:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "canGoBack" {
if webView.canGoBack {
// enable the back button
} else {
// disable the back button
}
}
}

最后,当用户点击后退按钮时,只需要调用:

popupWebView.goBack()

最新更新