Forked from fxm90/WebViewExampleViewController.swift
Created
August 27, 2018 11:20
-
-
Save drinkius/58cc8b4fd6fb2d1ebff42be8568e8ef3 to your computer and use it in GitHub Desktop.
Show progress of WKWebView in UIProgressBar that is attached to an UINavigationBar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ViewController.swift | |
| // | |
| // Created by Felix Mau on 06.01.18. | |
| // Copyright © 2018 Felix Mau. All rights reserved. | |
| // | |
| import UIKit | |
| import WebKit | |
| class ViewController: UIViewController { | |
| /// WebView from interface builder | |
| @IBOutlet weak var webView: WKWebView! | |
| /// Progress view showing the current loading progress of the web view | |
| let progressView = UIProgressView(progressViewStyle: .default) | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| navigationItem.title = "WKWebView - Sample App" | |
| setupProgressView() | |
| setupWebview(withUrl: "https://github.com/fxm90") | |
| } | |
| deinit { | |
| webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress)) | |
| webView.navigationDelegate = nil | |
| } | |
| // MARK: - UIProgressView | |
| func setupProgressView() { | |
| guard let navigationBar = navigationController?.navigationBar else { return } | |
| navigationBar.addSubview(progressView) | |
| progressView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin] | |
| progressView.alpha = 0.0 | |
| progressView.translatesAutoresizingMaskIntoConstraints = false | |
| NSLayoutConstraint.activate([ | |
| progressView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor), | |
| progressView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor), | |
| progressView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor), | |
| progressView.heightAnchor.constraint(equalToConstant: 2.0) | |
| ]) | |
| webView.addObserver(self, | |
| forKeyPath: #keyPath(WKWebView.estimatedProgress), | |
| options: .new, | |
| context: nil) | |
| } | |
| override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
| guard object as? WKWebView === webView, keyPath == #keyPath(WKWebView.estimatedProgress) else { return } | |
| progressView.progress = Float(webView.estimatedProgress) | |
| } | |
| // MARK: - WKWebView | |
| func setupWebview(withUrl urlAsString: String) { | |
| guard let url = URL(string: urlAsString) else { return } | |
| let request = URLRequest(url: url) | |
| webView.navigationDelegate = self | |
| webView.load(request) | |
| } | |
| } | |
| // MARK: - WKNavigationDelegate | |
| extension ViewController: WKNavigationDelegate { | |
| func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { | |
| UIApplication.shared.isNetworkActivityIndicatorVisible = true | |
| progressView.alpha = 0.0 | |
| UIView.animate(withDuration: 0.33, delay: 0.0, options: .curveEaseInOut, animations: { | |
| self.progressView.alpha = 1.0 | |
| }) | |
| } | |
| func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | |
| UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
| progressView.alpha = 1.0 | |
| UIView.animate(withDuration: 0.33, delay: 0.0, options: .curveEaseInOut, animations: { | |
| self.progressView.alpha = 0.0 | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment