Why my WKWebview inside my UIScrollView is not displayed?

不问归期 提交于 2021-01-29 05:32:02

问题


I need to add a WKWebview to load web content, inside the content view of UIScrollView.

I'm using the Apple code to load a web page, my outlet viewForWebview is linked correctly, and didFinish is called.

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    var webView: WKWebView!
    @IBOutlet var viewForWebview: UIView!

    override func loadView() {
        super.loadView()
        let myConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: myConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        viewForWebview = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.google.com.au")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Finished navigating to url \(String(describing: webView.url))")
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(error)
    }
}

But my the WKWebview is blank:

Just to test, I tried to display the WKWebview directly inside the content view of the scroll view, and it works.

Why is it not working as subview of the content?


回答1:


Don't override loadView for such actions , you need

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    var webView: WKWebView!
    @IBOutlet var viewForWebview: UIView!


    override func viewDidLoad() {
        super.viewDidLoad()


        let myConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame:.zero, configuration: myConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        viewForWebview.addSubview(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            webView.topAnchor.constraint(equalTo: viewForWebview.topAnchor),
            webView.bottomAnchor.constraint(equalTo: viewForWebview.bottomAnchor),
            webView.leadingAnchor.constraint(equalTo: viewForWebview.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: viewForWebview.trailingAnchor)

        ])

        let url = URL(string: "https://www.apple.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Finished navigating to url \(String(describing: webView.url))")
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(error)
    }
}



来源:https://stackoverflow.com/questions/54107973/why-my-wkwebview-inside-my-uiscrollview-is-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!