更改WKWebView的框架会在滑动导航时导致白屏

时间:2018-11-19 11:55:28

标签: ios swift uiscrollview wkwebview

WKWebView中使用默认的滑动导航手势时,我发现WKWebView的帧大小在导航过程中是否发生了更改,因此预览无法正常工作,并会显示白屏。有人知道如何解决吗?

enter image description here

我的示例代码:

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))
    }
}

0 个答案:

没有答案