Paul
Paul

Reputation: 481

How to hide/show UI Navigation Bar for certain UIViews

Please see the storyboard below: enter image description here

I need to dynamically hide UINavigationBar for UITableViewController "Zero" and show it for UITableViewController "One" and "Two".

What is the best approach?

Upvotes: 0

Views: 1540

Answers (3)

Luca Archidiacono
Luca Archidiacono

Reputation: 127

I implemented a method which hides the NavigationController very smooth after one second... I prefer this than just hiding it ;)

   var navigation: UINavigationController!

override func viewDidLoad() {
        navigation = navigationController!
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "animate", userInfo: nil, repeats: false)
    }

    func animate(){
        hideController(self.navigation)
    }

    func hideController(navigationController: UINavigationController){
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

            self.navigationController.alpha = 0.0

            }, completion: nil)
}


    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        if self.navigationController!.respondsToSelector("interactivePopGestureRecognizer") {
            timer.invalidate()
            UIApplication.sharedApplication().statusBarHidden = false
        }
    }

I hope I could help you

you could also put the hide function into another class, so you don't need to retype it in the other views.

Edit:

I forgot to mention.. When you go one view back, you need to set you NavigationController back to visible... Therefore create another method for example the function show and set it's alpha to 1.0

    func show(navigationController: UINavigationController){
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

            self.navigationController.alpha = 1.0

            }, completion: nil)
}

Upvotes: 0

ikbal
ikbal

Reputation: 1174

try this..

override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}

you could use viewWillDisappear to set it to visible again

Upvotes: 1

pbush25
pbush25

Reputation: 5248

You can set the hidden property of the UINavigationBar when the views appear...

self.navigationController?.navigationBar.hidden = true

Just do this in the views where you want the nav bar hidden

Upvotes: 0

Related Questions