Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Navigation bar color transition when pressing back button

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.

like image 470
Johnny Oin Avatar asked Nov 14 '16 10:11

Johnny Oin


People also ask

How do I change the color of the navigation bar back button in Swift?

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.

Can you change Iphone navigation bar?

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.


1 Answers

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
like image 52
Paulo Cesar Avatar answered Oct 02 '22 01:10

Paulo Cesar