UINavigationController仅隐藏navigationBar – 返回动画问题

我在UINavigationController有三个viewControllers。 在第二个我需要隐藏导航栏而不是后退按钮和其他栏按钮 。 出于这个原因,我不能使用isNavigationBarHidden = true
目前我正在处理以上内容:

第一个viewController:

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.navigationBar.barTintColor = Constants.kThemeRedColor self.navigationController?.navigationBar.tintColor = UIColor.white self.navigationController?.navigationBar.barStyle = .black self.navigationController?.navigationBar.isTranslucent = false } 

第二个viewController(仅隐藏导航栏):

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.view.backgroundColor = UIColor.clear } 

这里的问题是当我从第一个ViewController导航到第一个ViewController时,在第一个viewController消失并且分别出现的时间很短,我看到它上面有一个黑色的导航栏。 我知道这是因为在第二个viewController中编写的代码。 但我没有任何其他解决方案。 附上截图:

第一个viewController(应该如何):

在此处输入图像描述

第二个viewController:

在此处输入图像描述

第一个viewController(黑色导航栏持续时间很短):

在此处输入图像描述

First viewController ,还将navigationBarbackgroundImageshadowImage设置为nil ,即

 class FirstVC: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default) self.navigationController?.navigationBar.shadowImage = nil self.navigationController?.navigationBar.barTintColor = .red self.navigationController?.navigationBar.tintColor = UIColor.white self.navigationController?.navigationBar.barStyle = .black } } class SecondVC: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) self.navigationController?.view.backgroundColor = UIColor.clear self.navigationController?.navigationBar.shadowImage = UIImage() } } 

您应隐藏导航控制器的导航栏并使用UIView使用自定义导航栏。 我认为这将解决您的问题。

由于UIWindow的背景颜色正在发生导航条转换,因此第一个viewController在快速转换时显示黑色导航条的持续时间很短。 只需在AppDelegate的didFinishLaunchingWithOptions:方法中添加此行

 window?.backgroundColor = Constants.kThemeRedColor 

你完成了。 快乐的编码!