gpt4 book ai didi

swift - 从 UIWebView 切换到 WKWebView 委托(delegate)

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:15 25 4
gpt4 key购买 nike

什么相当于

webView.delegate = self

在 WKWebView 中?我有

webView2.UIDelegate = self

但它没有按预期工作。

最佳答案

您可以使用键值观察来监控 WKWebView 的 canGoBack 和 canGoForward。

swift 3

向 WKWebView 添加两个键值观察器。

let webView = WKWebView(frame: view.frame)
view.addSubview(webView)

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

现在,在您的 View Controller 中实现 observeValue(forKeyPath:) 方法。

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

if keyPath == #keyPath(WKWebView.canGoBack) || keyPath == #keyPath(WKWebView.canGoForward) {
// Do Something...
}
}

例如,您可以将类似这样的内容添加到按钮的更改色调中。

backButton.imageView?.tintColor = webView.canGoBack ? UIColor.blue : UIColor.gray
forwardButton.imageView?.tintColor = webView.canGoBack ? UIColor.blue : UIColor.gray

我通过阅读这里的一篇文章找到了这个方法。

https://www.hackingwithswift.com/example-code/wkwebview/how-to-monitor-wkwebview-page-load-progress-using-key-value-observing

关于swift - 从 UIWebView 切换到 WKWebView 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37193903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com