I'm currently attempting to make a global activity indicator in swift, which is called whenever a fetch to the api is made. The idea is that the activity indicator will appear in the top left of a navigation bar (navigation view controller's child), and is available on every app page.
Can't show example image due to new account / low rep
I have the activity indicator displaying correctly, I'm just not sure on how to make it available from any page on the app - i've considered an extension, but am not sure on what the best way to approach it is. Any help would be greatly appreciated.
Activity Indicator Code:
let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
var activityItem = UIBarButtonItem()
func navBarActivity() {
// Call navBarActivity() to start activity indicator
// Use navigationItem.leftBarButtonItem = nil to stop activity indicator
activityIndicator.startAnimating()
activityIndicator.hidden = false
self.activityItem = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.leftBarButtonItem = activityItem
}
I would recommend extension like this.
extension UIViewController {
func displayNavBarActivity() {
let indicator = UIActivityIndicatorView(activityIndicatorStyle: .White)
indicator.startAnimating()
let item = UIBarButtonItem(customView: indicator)
self.navigationItem.leftBarButtonItem = item
}
func dismissNavBarActivity() {
self.navigationItem.leftBarButtonItem = nil
}
}
Call self.displayNavBarActivity()
before api call, and call self.dismissNavBarActivity()
after api call done.
However, I want you to check networkActivityIndicatorVisible
of UIApplication
. Consider using this option.
Specify YES if the app should show network activity and NO if it should not. The default value is NO. A spinning indicator in the status bar shows network activity. The app may explicitly hide or show this indicator.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instp/UIApplication/networkActivityIndicatorVisible
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With