我的应用程序中有多个视图控制器。我想在我的第一个视图控制器中隐藏
navigationbar
。所以我使用下面的代码来隐藏导航栏
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true);
现在我想在其他视图控制器中添加导航栏,但是我的导航栏在该视图控制器中不可见。为什么会这样?
我的故事板显示了导航栏,但一旦我尝试运行我的应用程序,它就消失了。
如果我从一个视图控制器隐藏导航栏,那么我们就无法使用导航控制器,是这样吗?我希望我错了。那么导航栏不显示的原因有哪些呢?
编辑:
我也希望我的视图控制器仅处于纵向模式。所以我做了以下操作是否导致了问题?
extension UINavigationController{
public override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false
}
else {
return true
}
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}
}
编辑1:
我正在使用以下代码从一个视图控制器移动,而不是从故事板链接。现在这会引起问题吗?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)
编辑2:
请检查我的以下屏幕截图。这是我对 secondaryview 控制器的设置
编辑3:
Navigation Controller 是一个控制器,它有一堆视图控制器。所以如果你有这样的事情:
NAV -> A ->(继续)B
即使您隐藏导航栏,您仍然应该能够进行转场。另外,您不能在 viewWillAppear 的第二个 (B) 视图控制器中取消隐藏导航栏吗?首先以同样的方式将其隐藏在 viewWillAppear 上。
编辑:问题的最终解决方案: 用途:
let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
self.navigationController!.pushViewController(controller)
而不是:
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)
因为pushViewController会将secondViewController添加到它的堆栈中。 presentViewController 正在替换您的导航控制器,这就是您看不到导航栏的原因。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
在您不想显示导航栏的视图控制器的 viewDidLoad 方法中添加行
navigationController.navigationBarHidden = true
您目前隐藏在所有视图控制器中
编辑:您正在呈现视图控制器,而不是它应该是
self.navigationController!.pushViewController(controller)
就像在
viewcontroller
中使用 Swift 4.0 隐藏一样
将
navigationController
隐藏在 viewWillAppear
中
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
取消隐藏
navigationController
中的 viewWillDisappear
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
我的 swift 项目也有同样的要求。
这就是我处理导航栏的方式
确保您的第一个屏幕嵌入到导航控制器中
例如我们有两个屏幕 A 和 B
在屏幕 A 中,您需要在 viewWillAppear
中隐藏导航栏override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//hide navigation for screen A
self.navigationController?.navigationBarHidden = true
}
用于在屏幕 B 中启用导航 您需要在屏幕 A 中添加以下代码
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
if (segue.identifier == "screen B's segue identifier here")
{
//enable navigation for screen B
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
}
}
使用上面的样式,我可以随时启用或禁用特定屏幕的导航栏
如果您需要仅在该控制器中隐藏此导航栏,最好的方法是在
viewWillDisappear()
中显示它并在 viewWillAppear()
中隐藏。
现在回复已经太晚了,还有其他很好的答案,但我想分享对我有用的答案。
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVC")
self.navigationController!.pushViewController(controller!, animated:true)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let navigationBarAppearace = UINavigationBar.appearance()
// navigationBarAppearace.tintColor = .tintColor
navigationBarAppearace.barTintColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0)
navigationBarAppearace.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0) //<insert your color here>
navigationBarAppearace.standardAppearance = navBarAppearance // have a look here ;). Required to solve navigation bar appearing only on scrolling.
navigationBarAppearace.scrollEdgeAppearance = navBarAppearance
}
return true
}
// 并将其添加到您的 viewController 中,ViewDidLoad
self.navigationController?.navigationBar.prefersLargeTitles = true
// 这在 2025 年 12 月,Xcode 16 对我有用。