iOS - 按下后退按钮时导航栏颜色转换
iOS - Navigation bar color transition when pressing back button
我试图在导航堆栈上推送视图控制器时更改导航栏的颜色,在 navigationController(_:willShow:animated:) 期间使用 barTintColor。
代码如下:
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)
}
}
当我按下视图控制器和使用向后滑动手势时(两种方式的颜色过渡都很平滑),一切都很好。
但是当我按下后退按钮时,一开始颜色没有改变,导航过渡完成,然后颜色改变,没有动画.
是否有人遇到/解决了这个问题?任何线索将不胜感激。
我遇到了完全相同的问题,所以用自定义左栏按钮替换了 "back" 按钮,并调用了:
navigationController?.popViewController(animated: true)
编辑:
设置 leftBarButton 导致滑动手势丢失,所以我需要另一个技巧:
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(pop))
navigationController?.interactivePopGestureRecognizer?.delegate = self
我最终使用了 KMNavigationBarTransition library,它运行良好并且不需要一行代码。
我试图在导航堆栈上推送视图控制器时更改导航栏的颜色,在 navigationController(_:willShow:animated:) 期间使用 barTintColor。
代码如下:
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)
}
}
当我按下视图控制器和使用向后滑动手势时(两种方式的颜色过渡都很平滑),一切都很好。
但是当我按下后退按钮时,一开始颜色没有改变,导航过渡完成,然后颜色改变,没有动画.
是否有人遇到/解决了这个问题?任何线索将不胜感激。
我遇到了完全相同的问题,所以用自定义左栏按钮替换了 "back" 按钮,并调用了:
navigationController?.popViewController(animated: true)
编辑:
设置 leftBarButton 导致滑动手势丢失,所以我需要另一个技巧:
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(pop))
navigationController?.interactivePopGestureRecognizer?.delegate = self
我最终使用了 KMNavigationBarTransition library,它运行良好并且不需要一行代码。