在WKWebView
中使用默认的滑动导航手势时,我发现WKWebView
的帧大小在导航过程中是否发生了更改,因此预览无法正常工作,并会显示白屏。有人知道如何解决吗?
我的示例代码:
import UIKit
import WebKit
class ViewController: UIViewController {
private lazy var webView: WKWebView = {
let view = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
view.allowsBackForwardNavigationGestures = true
view.navigationDelegate = self
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.addSubview(self.webView)
if let url = URL(string: "http://www.wikipedia.org") {
self.webView.load(URLRequest(url: url))
}
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame = CGRect(origin: webView.frame.origin, size: CGSize(width: webView.frame.width, height: webView.frame.height - 20))
}
}