在 swift 4 中使用 webkit 在 webView 中导航时创建活动指示器
Posted
技术标签:
【中文标题】在 swift 4 中使用 webkit 在 webView 中导航时创建活动指示器【英文标题】:create activity indicator when navigate in webView using webkit in swift 4 【发布时间】:2018-03-09 08:07:22 【问题描述】:我想在我的 ios 应用程序中创建一个 wkwebview,并且我想向它添加一个活动指示器。我希望当我们单击 webview 中的所有按钮或部分时,它会出现活动指示器。你能给我一些代码来做到这一点吗?这是我现在的代码。它只在开始加载网站时显示指示器,但当我们点击该过程时,指示器不出现。
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate,UIWebViewDelegate
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var webView: WKWebView!
@IBOutlet var containerView: UIView? = nil
override func viewDidLoad()
super.viewDidLoad()
self.view.addSubview(self.activityIndicator)
self.view.addSubview(self.webView)
let url:URL = URL(string : "https://www.facebook.com/")!
let urlRequest : URLRequest = URLRequest(url: url)
webView.load(urlRequest)
//activity indicator
self.webView.addSubview(self.activityIndicator)
self.activityIndicator.startAnimating()
self.webView.navigationDelegate = self
self.activityIndicator.hidesWhenStopped = true
//refresh
webView.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear( animated )
let url:URL = URL(string : "https://www.facebook.com/")!
let urlRequest : URLRequest = URLRequest(url: url)
webView.load(urlRequest)
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
activityIndicator.stopAnimating()
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
activityIndicator.stopAnimating()
@objc func refreshWebView(sender: UIRefreshControl)
print("refersh")
sender.endRefreshing()
【问题讨论】:
【参考方案1】:以下代码将有助于在导航栏中添加活动指示器。所以不需要创建任何子视图。这适用于 Swift 4。
import Foundation
import UIKit
import WebKit
class ContactVC: UIViewController, WKNavigationDelegate
var webView: WKWebView!
var activityIndicator: UIActivityIndicatorView!
override func loadView()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.hidesWhenStopped = true
let barButton = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.setRightBarButton(barButton, animated: true)
override func viewDidLoad()
super.viewDidLoad()
let url = URL(string: "https://www.facebook.com/")!
webView.load(URLRequest(url: url))
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func showActivityIndicator(show: Bool)
if show
// Start the loading animation
activityIndicator.startAnimating()
else
// Stop the loading animation
activityIndicator.stopAnimating()
//MARK:- WKNavigationDelegate
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
print("Start to load")
showActivityIndicator(show: true)
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
print("Finish to load")
title = webView.title
showActivityIndicator(show: false)
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
print(error.localizedDescription)
showActivityIndicator(show: false)
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)
print(error.localizedDescription)
showActivityIndicator(show: false)
【讨论】:
【参考方案2】:实现 didStartProvisionalNavigation 委托函数。
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
activityIndicator.startAnimating()
【讨论】:
非常感谢先生 :)以上是关于在 swift 4 中使用 webkit 在 webView 中导航时创建活动指示器的主要内容,如果未能解决你的问题,请参考以下文章