UINavigationBar change colors on push

后端 未结 4 1971
长情又很酷
长情又很酷 2020-12-23 17:49

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          


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 17:58

    I am just wondering. For the same purpose I use UINavigationControllerDelegate. In navigationController(_:willShow:) I start the animation using transitionCoordinator?.animate(alongsideTransition:completion:). It works great when pushing new controllers, however pop doesn't.

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
      let dst = viewController as! ViewController
      guard animated else {
        navigationController.navigationBar.barTintColor = dst.navigationBarColor
        navigationController.navigationBar.tintColor = dst.tintColor
        navigationController.navigationBar.barStyle = dst.barStyle
        return
      }
    
      navigationController.transitionCoordinator?.animate(alongsideTransition: { context in
        navigationController.navigationBar.barTintColor = dst.navigationBarColor
        navigationController.navigationBar.tintColor = dst.tintColor
        navigationController.navigationBar.barStyle = dst.barStyle
      }, completion: { context in
        if context.isCancelled {
          let source = context.viewController(forKey: UITransitionContextViewControllerKey.from) as! ViewController
            navigationController.navigationBar.barTintColor = source.navigationBarColor
            navigationController.navigationBar.tintColor = source.tintColor
            navigationController.navigationBar.barStyle = source.barStyle
        }
    })
    

    Do you see any reason why it should work with pushes but not pops?

提交回复
热议问题