如何在加载应用程序时以及在完全加载Web视图页之前直接添加启动屏幕图像?

时间:2019-07-02 18:45:45

标签: ios swift wkwebview

我正在创建IOS应用,并且正在寻找在完全加载Web视图页面之前显示初始屏幕的方法,该怎么办?

import UIKit
import WebKit

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

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()


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


        let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
        toolbarItems = [refresh]
        navigationController?.isToolbarHidden = false
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        title = webView.title
    }

}

1 个答案:

答案 0 :(得分:0)

一种方法:添加一个UIImageView覆盖整个视图(或至少覆盖Web视图的框架)。实现WKNavigationDelegate。当通知您导航已完成时,请删除图像视图。

class ViewController: UIViewController, WKNavigationDelegate {

    var theWebView: WKWebView!
    var splashImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        theWebView = WKWebView()
        splashImageView = UIImageView()

        theWebView.translatesAutoresizingMaskIntoConstraints = false
        splashImageView.translatesAutoresizingMaskIntoConstraints = false

        // load splash image
        if let img = UIImage(named: "mySplashImage") {
            splashImageView.image = img
        }

        // add the web view and the splash image view
        view.addSubview(theWebView)
        view.addSubview(splashImageView)

        NSLayoutConstraint.activate([

            // constrain the web view to all 4 sides, with 20-pts "padding"
            theWebView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20.0),
            theWebView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20.0),
            theWebView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20.0),
            theWebView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20.0),

            // constrain the splash image view to all 4 sides of the web view
            splashImageView.topAnchor.constraint(equalTo: theWebView.topAnchor),
            splashImageView.bottomAnchor.constraint(equalTo: theWebView.bottomAnchor),
            splashImageView.leadingAnchor.constraint(equalTo: theWebView.leadingAnchor),
            splashImageView.trailingAnchor.constraint(equalTo: theWebView.trailingAnchor),

            ])

        // set navigation delegate
        theWebView.navigationDelegate = self

        // load a url
        if let url = URL(string: "https://apple.com") {
            theWebView.load(URLRequest(url: url))
        }

    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        // for debugging
        print("Loaded")
        // remove the splash image view
        splashImageView.removeFromSuperview()
    }
}

注意:

  • 这是示例代码,仅能助您一臂之力-不应被视为“生产就绪”
  • 您要处理错误(错误的URL等)
  • 您可能想要添加一个微调器(UIActivityIndicatorView)或进度条,以使用户知道该应用程序正在等待网页加载。

如果想走这条路,这里是获得估算的装载进度的示例:https://www.hackingwithswift.com/example-code/wkwebview/how-to-monitor-wkwebview-page-load-progress-using-key-value-observing