I'm using 2 different bar tint colors at UINavigationBar
in different views. I'n changing color with that method in both views:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = COLOR
}
When I tap on back button color is not changed smoothly (you can see blink on last second).
But everything is okay if just swipe view back instead of tapping on back button.
How to make smooth transition in both situations?
I've coded final solution that looks most comfortable to use (don't need to use a lot of overrides in own view controllers). It works perfectly at iOS 10 and easy adoptable for own purposes.
GitHub
You can check GitHub Gist for full class code and more detailed guide, I won't post full code here because Stackoverflow is not intended for storing a lot of code.
Usage
Download Swift file for GitHub. To make it work just use
ColorableNavigationController
instead ofUINavigationController
and adopt needed child view controllers toNavigationBarColorable
protocol.Example:
I am just wondering. For the same purpose I use
UINavigationControllerDelegate
. InnavigationController(_:willShow:)
I start the animation usingtransitionCoordinator?.animate(alongsideTransition:completion:)
. It works great when pushing new controllers, however pop doesn't.Do you see any reason why it should work with pushes but not pops?
To achieve this kind of animation you should use
UIViewControllerTransitionCoordinator
as Apple documentation say it is :So every
UIViewController
has owntransitionController
. To get this you should call in theUIViewControllerClass
:self.transitionCoordinator()
From documentation:
So to get the result that you want you should implement
animateAlongsideTransition
method in viewController transitionCoordinatior. Animation works when you clickbackButton
and swipe to back.Example :
First Controller :
Second Controller:
UPDATE iOS 10
In the iOS 10 the tricky part is to add the
willMoveTo(parentViewController parent: UIViewController?)
in the second ViewController. And set the navigationBartintColor
to the color value of previous controller. Also, inviewDidAppear
method in second ViewControler set thenavigationBar.tintColor
to the color from second viewController.Check out my example project on github