简体   繁体   中英

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.

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

我最终使用了KMNavigationBarTransition库 ,它运行良好,不需要一行代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM