I want to have an UIProgressView to show a progress on the bottom of a navigation bar (just like when sending an iMessage or text message in iOS 7). But I need this consistently on every table view view of my navigation controller. So for me it was clear: I have to add this to the UINavigationController. But the problem is, it's not possible to add an UIProgressView to the UINavigationController. So I tried out two things:
1st I tried to add it to UINavigationController's view programmatically. But the problem was to position the UIProgressView and to make it look good when changing device rotation.
The 2nd thing I tried is to add the UIProgressView to every UITableView, but then I really have to do this for every view. Also it doesn't look good, because it is not on top of the navigation bar but beneath it. But the main reason why I didn't like the 2nd solution is because the ProgressViews go and come with their TableView, so you don't have a static one but changing ones.
After this, I don't have any idea to do this, so I ask you… does anyone have an idea how to do this?
That's how it should look like:
Solution for SnapKit users. Also places the
progressView
above thenavigationBar
.You can add ProgressBar in titleView of UInavigationController as displayed in below screenshot:
OUTPUT :
Hope it helps you.
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:
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 theUIProgressView
with the optionalprogressView
property. There may be more elegant ways to do that, but this seems the most straightforward)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:
You can do this in the root viewController of the navigationController:
But if you want always in the nav bar the same progress bar for all the pushed VCs then it is better to subclass UINavigationController and:
I've outlined how to do this in my blog post here
TL;DR
create protocol which presents a UIProgressView as UINavigationController subview
create protocol which updates UIProgressView
I reworked the original poster's answer so that the bar is actually just inside the navigation bar. What's nice about this is that when its showing, it overlaps the one pixel bottom line (in effect replacing it), so when you animate the progress bar to hidden, the progress bar fades out and the separator line fades in. The key part of this solution is adding the progress bar to the Navigation Controller's view, not the Navigation bar.
I'm not sure why its necessary to add the 0.5 offset to the NSLayoutContstraints code to get the same match, but it is. I use these not the visual formats, but the choice is yours. Note that contraining to the bottoms makes this seamless in rotation too.