Building on what's already been suggested here, if you want to make it display on every navigation bar in the app, you can make it into an extension (Swift) on UINavigationController:
extension UINavigationController {
public override func viewDidLoad() {
super.viewDidLoad()
let progressView = UIProgressView(progressViewStyle: .Bar)
self.view.addSubview(progressView)
let navBar = self.navigationBar
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[navBar]-0-[progressView]", options: .DirectionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView, "navBar" : navBar]))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[progressView]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView]))
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.setProgress(0.5, animated: false)
}
}
UPDATE (Uses Swift 3 Syntax)
Here is a bit more complete solution. I put this extension into a file called UINavigationController+Progress.swift. (Notice I'm using the UIView
tag property to find the UIProgressView
with the optional progressView
property. There may be more elegant ways to do that, but this seems the most straightforward)
import UIKit
let kProgressViewTag = 10000
let kProgressUpdateNotification = "kProgressUpdateNotification"
extension UINavigationController {
open override func viewDidLoad() {
super.viewDidLoad()
let progressView = UIProgressView(progressViewStyle: .bar)
progressView.tag = kProgressViewTag
self.view.addSubview(progressView)
let navBar = self.navigationBar
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[navBar]-0-[progressView]", options: .directionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView, "navBar" : navBar]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[progressView]|", options: .directionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView]))
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.setProgress(0.0, animated: false)
NotificationCenter.default.addObserver(self, selector: #selector(UINavigationController.didReceiveNotification(notification:)), name: NSNotification.Name(rawValue: kProgressUpdateNotification), object: nil)
}
var progressView : UIProgressView? {
return self.view.viewWithTag(kProgressViewTag) as? UIProgressView
}
func didReceiveNotification(notification:NSNotification) {
if let progress = notification.object as? ProgressNotification {
if progress.current == progress.total {
self.progressView?.setProgress(0.0, animated: false)
} else {
let perc = Float(progress.current) / Float(progress.total)
self.progressView?.setProgress(perc, animated: true)
}
}
}
}
class ProgressNotification {
var current: Int = 0
var total: Int = 0
}
So I've given a specific implementation here that assumes you want a current count and a total count value to be used to update the progress bar. Now, what you need is to post the notification from the code that is performing whatever tasks are to be used to determine progress--for example downloading a list of files. Here's the code to post the notification:
let notification = ProgressNotification()
notification.current = processedTaskCount
notification.total = totalTaskCount
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kProgressUpdateNotification), object: notification)
}