Hide navigation bar in specific view - Swift 3

I have a NavigationController that handles navigation through my application. According to my design, the very first view should not have a visible NavigationBar. All the rest after will be.

In this FirstView, I use it so far to hide the NavBar inside the ViewDidLoad:

self.navigationController?.isNavigationBarHidden = true

From this FirstView, I can access other views. In these other views, I show NavBar using:

self.navigationController?.isNavigationBarHidden = false

My problem is that:

  • When I navigate from View with Visible NavBar, back to FirstView using the hidden NavBar, the NavBar is now visible. Basically, NavBar only hides for the first time, and then shows if I use the back button.

How can I prevent this?

Thank!

+4
3

viewWillAppear() viewDidLoad().

viewDidLoad() , viewWillAppear() , .

.

+6

FirstViewController viewWillAppear.

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

SecondViewController viewWillAppear

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

viewWillAppear viewWillDisappear FirstViewController.

+2

You can use this function to hide the NavigationBar with cool animations:

 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 the NavigationBar, set it to β€œTrue”, and if you want to call the NavigationBar again, set it to β€œFalse”

+1
source

Source: https://habr.com/ru/post/1676137/


All Articles