查看子视图不可见

时间:2018-07-17 10:33:00

标签: ios iphone swift

我在主视图中添加了一个WKWebView,但是由于某种原因,当我运行该应用程序时,它会给我一个白屏。我是否需要为viewwebView变量设置一些额外的大小变量?

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        view.frame = view.bounds
        webView = WKWebView(frame: CGRect( x: 0, y: 20, width: 380, height: 150 ), configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        view.addSubview(webView)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

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

2 个答案:

答案 0 :(得分:0)

UIViewController.loadView文档的第一行说:

  

创建控制器管理的视图

这意味着loadView中没有mainView,在这里您必须为UIViewController分配/加载视图。

因此,您必须替换此行:

view.addSubview(webView)

此行:

self.view = webView

无论如何,这是您使用loadView的固定视图控制器:

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .null, configuration: webConfiguration)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        self.view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

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

请注意,我更改了frame: .null,这也由ViewController管理。

这里是另一个类,向您展示如何使用addSubview而不是loadView来完成

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView = WKWebView(frame: CGRect(x: 0, y: 20, width: 380, height: 150))
        self.webView.navigationDelegate = self
        self.webView.uiDelegate = self
        self.view.addSubview(webView)

        if let url = URL(string: "https://www.google.com") {
            self.webView.load(URLRequest(url: url))
        }
    }
}

答案 1 :(得分:0)

loadView()

  

永远不要直接调用此方法。视图控制器调用   当请求其view属性但当前为nil时,此方法。   此方法加载或创建一个视图并将其分配给该视图   财产

您可以尝试使用viewDidLoad

import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
        var webView: WKWebView!           
        override func viewDidLoad() {
            super.viewDidLoad()
            let webConfiguration = WKWebViewConfiguration()
            view.frame = view.bounds
            webView = WKWebView(frame: CGRect( x: 0, y: 20, width: UIScreen.main.bounds.width, height: 250 ), configuration: webConfiguration)
            webView.navigationDelegate = self
            webView.uiDelegate = self
            view.addSubview(webView)
            let myURL = URL(string: "https://www.google.com")
            let myRequest = URLRequest(url: myURL!)
            webView.load(myRequest)
        }
    }