Ios 以编程方式添加WKWebView

Ios 以编程方式添加WKWebView,ios,wkwebview,activity-indicator,Ios,Wkwebview,Activity Indicator,我是iOS开发的初学者,一直很难用代码添加WKWebview。我打算让它全宽,但我得到了一个空屏幕 var webView = WKWebView() var activityIndicatorView: ActivityIndicatorView! override func viewDidLoad() { super.viewDidLoad() //show Activity Indicator self.activityIndicatorView = Acti

我是iOS开发的初学者,一直很难用代码添加WKWebview。我打算让它全宽,但我得到了一个空屏幕

var webView = WKWebView()
var activityIndicatorView: ActivityIndicatorView!

override func viewDidLoad() {
    super.viewDidLoad() 
     //show Activity Indicator
    self.activityIndicatorView = ActivityIndicatorView(title: "Loading content...", center: self.view.center)
    self.activityIndicatorView.startAnimating();
    self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())
    // Do any additional setup after loading the view.
    let urlString = "http://www.youtube.com";
    let request = URLRequest(url:URL(string: urlString)!)
    self.webView.load(request)
     self.view = webView
}

您正在创建一个零帧的webView,这就是它不显示的原因

webView=WKWebView(帧:.0)
更改为:

webView = WKWebView(frame: self.view.frame)
class ViewController: UIViewController, WKNavigationDelegate {
    var webView = WKWebView()
    var activityIndicatorView: ActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad() 
         //show Activity Indicator
        self.activityIndicatorView = ActivityIndicatorView(title: "Loading content...", center: self.view.center)
        self.activityIndicatorView.startAnimating();
        self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())
        // Do any additional setup after loading the view.
        let urlString = "http://www.youtube.com";
        let request = URLRequest(url:URL(string: urlString)!)
        self.webView.navigationDelegate = self // Add this line!
        self.webView.load(request)
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.view = webView
        // Hide activity indicator here
    }
}
要在加载页面时显示活动指示器,需要实现
WKNavigationDelegate
delegate。将您的代码更新为:

webView = WKWebView(frame: self.view.frame)
class ViewController: UIViewController, WKNavigationDelegate {
    var webView = WKWebView()
    var activityIndicatorView: ActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad() 
         //show Activity Indicator
        self.activityIndicatorView = ActivityIndicatorView(title: "Loading content...", center: self.view.center)
        self.activityIndicatorView.startAnimating();
        self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())
        // Do any additional setup after loading the view.
        let urlString = "http://www.youtube.com";
        let request = URLRequest(url:URL(string: urlString)!)
        self.webView.navigationDelegate = self // Add this line!
        self.webView.load(request)
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.view = webView
        // Hide activity indicator here
    }
}
这样使用:

lazy var webView: WKWebView = {
    let wv = WKWebView()
    wv.uiDelegate = self
    wv.navigationDelegate = self
    wv.translatesAutoresizingMaskIntoConstraints = false
    return wv
}()


override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(webView)
    NSLayoutConstraint.activate([
        webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        webView.topAnchor.constraint(equalTo: view.topAnchor),
        webView.rightAnchor.constraint(equalTo: view.rightAnchor),
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor)])
}

添加约束。或者给它一个框架。您应该在分配
self.view=webview
后添加指示器,或者在侧边webview中添加指示器视图。当您将webview指定给self.view时,self.view将被webview替换,并且webview没有活动指示器视图您的代码可以工作,但它涵盖了我作为子视图添加的活动指示器too@bennycodest你的活动指示器的框架是什么?请分享它的代码。你什么时候把它藏起来?@bennycodest我已经更新了答案。请注意,我已将
WKNavigationDelegate
添加到类声明中,并将
navigationDelegate
添加到
viewDidLoad