Reputation: 2821
I have a navigation stack like this : TabBarController -> NavigationController->ControllerA-> ControllerB.
The ControllerA and ControllerB are pushed into the NavigationController. And I want the ControllerA's navigationBar's backgroundColor is RED, and the ControllerB's navigationBar's backgroundColor is GREEN.
I am already tried the appearanceWhenContainedInInstancesOfClasses. But because the bar is belonged to the NavigationController. it was above the ControllerA& ControllerB in the hierarchy . So this appearanceWhenContainedInInstancesOfClasses seems can't work.
Is there anyway to make this work with UINavigationBar appearance?
Upvotes: 0
Views: 32
Reputation: 15058
I would make each view controller responsible for setting the nav bar's color in its viewWillAppear
method. This ensures the nav bar has the appropriate color based on which view controller is in view.
Instead of using the appearance...
methods, setup the colors directly on the specific nav bar. Each view controller can access the nav bar via:
self.navigationController?.navigationBar
If you'd rather have some other class control the colors, you could have the class set itself as the navigation controller's delegate
and implement the navigationController(_:willShow:animated:)
delegate method. Set the nav bar color based on which controller will be shown.
Upvotes: 0