简体   繁体   English

在特定视图中隐藏导航栏 - Swift 3

[英]Hide Navigation Bar in Specific View - Swift 3

I have NavigationController that handles navigation through my app. 我有NavigationController处理我的应用程序导航。 According to my design, the very first view should have no visible NavigationBar. 根据我的设计,第一个视图应该没有可见的NavigationBar。 All the others after, will. 所有其他人之后,将会。

In this FirstView, I'm using this so far to hide the NavBar, inside the ViewDidLoad: 在这个FirstView中,我到目前为止使用它来隐藏ViewDidLoad中的NavBar:

self.navigationController?.isNavigationBarHidden = true

From this FirstView I can access other Views. 从这个FirstView我可以访问其他视图。 In these other views I show the NavBar using: 在这些其他视图中,我使用以下命令显示NavBar:

self.navigationController?.isNavigationBarHidden = false

My problem is that: 我的问题是:

  • When I navigate from a View with Visible NavBar, back to the FirstView with the Hidden NavBar, the NavBar is now visible. 当我从具有Visible NavBar的View导航回到带有Hidden NavBar的FirstView时,NavBar现在可见。 Basically the NavBar only hides the very first time then shows if I use the back button. 基本上NavBar只隐藏了第一次然后显示我是否使用后退按钮。

How Can I Prevent this ? 我怎么能防止这个?

Thank you! 谢谢!

Move that code to viewWillAppear() instead of viewDidLoad() . 将该代码移动到viewWillAppear()而不是viewDidLoad()

viewDidLoad() is only called once per instantiated view controller, whereas viewWillAppear() is called whenever the view controller is about to be presented on screen. viewDidLoad()仅在每个实例化的视图控制器中调用一次,而每当视图控制器即将在屏幕上显示时,都会调用viewWillAppear()

You can read more about the view controller lifecycle here . 您可以在此处阅读有关视图控制器生命周期的更多信息

Write below code in your FirstViewController 's viewWillAppear method. FirstViewControllerviewWillAppear方法中写下面的代码。

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = true
}

And in your SecondViewController 's viewWillAppear method write below code 并在你的SecondViewControllerviewWillAppear方法中编写下面的代码

 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}

Do not try to hide and show nav bar in viewWillAppear and viewWillDisappear subsequetly in FirstViewController . 不要试图隐藏和显示导航在viewWillAppear中viewWillDisappearFirstViewController酒吧subsequetly。

You can use this function to hide NavigationBar with cool animation: 您可以使用此功能隐藏带有炫酷动画的NavigationBar:

 func setupAnimationForNavigationBar(caseOfFunction: Bool) {
    if caseOfFunction == true {
        UIView.animate(withDuration: 0.5) {
            self.navigationController?.navigationBar.transform = CGAffineTransform(translationX: 0, y: -200)
        }
    } else {
        UIView.animate(withDuration: 0.5, animations: {
            self.navigationController?.navigationBar.transform = CGAffineTransform.identity
        })
    }

}

If you want to hide NavigationBar, so set it "True" and if you want to call NavigationBar again, set it "False" 如果要隐藏NavigationBar,请将其设置为“True”,如果要再次调用NavigationBar,请将其设置为“False”

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

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