代码之家  ›  专栏  ›  技术社区  ›  cmii

为什么我的UIScrollView中的WKWebview不显示?

  •  0
  • cmii  · 技术社区  · 5 年前

    我需要在UIScrollView的内容视图中添加一个WKWebview来加载web内容。

    enter image description here

    我正在使用苹果代码加载一个网页,我的插座 viewForWebview 已正确链接,并且 didFinish 被称为。

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

    但我的WKWebview是空白的:

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  0
  •   Shehata Gamal    5 年前

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

    enter image description here