I'm trying to change the color of the navigation bar when pushing a view controller on the navigation stack, using barTintColor during navigationController(_:willShow:animated:).
Here is the code:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController is ViewerViewController {
navigationBar.barTintColor = UIColor(custom: .white)
navigationBar.tintColor = UIColor(custom: .black)
} else if viewController is FeedViewController {
navigationBar.barTintColor = UIColor(custom: .blue)
navigationBar.tintColor = UIColor(custom: .white)
}
}
Everything works beautifully when I push the view controller and when I use the swipe back gesture (color transition is smooth in both ways).
However when I press the back button, color doesn't change at first, the navigation transition is done, and then color is changed with no animation.
Did anyone already encountered / resolved this issue ? Any clue would be appreciated.
To change the color of the navigation bar and the title text, we can use UINavigationBarAppearance, which is available since iOS 13. We hereby make a class Theme with a static method as follows, which will come in handy when we need it for some setup in our View or elsewhere.
Change the Bar StyleA user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.
I had the exact same problem, so replaced the "back" button with a custom left bar button, and called:
navigationController?.popViewController(animated: true)
edit:
setting the leftBarButton was causing the loss of the swipe gesture, so I needed another hack:
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(pop))
navigationController?.interactivePopGestureRecognizer?.delegate = self
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With