How can I make a certain UI only show up on some view controllers?

52 Views Asked by At

I have a UIProgressView that I wish to display on two view controllers, A and B. I am currently using a UITabBarController with 4 view controllers, such that I can navigate to any of the four from the navigation tab. The issue is that I want this progress view to only be displayed on 2 of the 4 view controllers.

My current approach is to build the same progress view separately on A and B. But the issue is that when I navigate from A to B or vice verse, the progress view does not increase smoothly, but updates in a very 'jumpy' manner where it goes to 0 before updating to the current progress.

In the tab bar controller I do

self.updateProgressView(prog)

inside of a completion handler as I need to wait for data before I can compute prog.

In my VCs I do

VC.updateProgressView = {prog in 
   progressBar.setProgress(prog)
}
1

There are 1 best solutions below

1
Duncan C On

A tab bar controller manages separate persistent view controllers. Each view controller has it’s own view hierarchy. When you switch tabs, the tab bar controller switches to displaying that tab’s view controller and it’s views.

You can’t have the same progress view appear on multiple tabs. You need to set up separate progress views for each tab where you want it to be shown.

What you can do is implement viewWillAppear methods on each tab’s view controller that sets the progress view to the current progress value, so that by the time it is shown, it already has the desired progress value. You will need some way to communicate the progress value between view controllers.