简体   繁体   English

iOS - 按下后退按钮时的导航栏颜色转换

[英]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:). 在导航堆栈中使用barTintColor时,我正在尝试更改导航栏的颜色,使用barTintColor(_: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: 设置leftBarButton导致滑动手势丢失,所以我需要另一个hack:

navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(pop))
navigationController?.interactivePopGestureRecognizer?.delegate = self

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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