30

I am having multiple view controller in my application. I want to hide navigationbar in my first view controller. So I use the following code to hide the navigation bar

navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true);

Now I want to add navigation bar in some other viewController but, my navigation bar not visible in that viewcontroller. Why it is happening?

My storyboard showing the navigation bar but once I try to run my application it is gone.

If I hide navigation bar from one view controller then we can't use navigation controller, Is it so? I hope I am wrong. Then what are the reasons for navigation bar not shown?

EDIT:

Also I want my view controller in portrait mode only. So I did the following Is that causing the issue?

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]
    }
    

}

Edit 1:

I am using following code to move from one view controller not link from the storyboard. Is that causing issue now?

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
        presentViewController(secondViewController, animated: false, completion: nil)

Edit 2:

Please check my following screenshots. Which are my settings for secondview controller

enter image description here

enter image description here

Edit 3:

Here is my navigation controller attribute inspector enter image description here

Community
  • 1
  • 1
Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • you are setting on whole navigation controller stack so it will not be visible on view controllers which are placed on navigation controller. If you need to disable only on certain view controllers just do [self.navigationController setNavigationBarHidden:YES]; in viewDidLoad method of the view controller – harsha yarabarla Nov 13 '15 at 09:16

7 Answers7

39

Navigation Controller is a controller, which has stack of view controllers. So if you have something like this:

NAV -> A -> (segue) B

Even if you'll hide navigation bar you still should be able to make segues. Also can't you just unhide navigation bar in second (B) view controller in viewWillAppear? And in first the same way hide it on viewWillAppear.

edit: Final solution to the problem: Use:

 let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
 self.navigationController!.pushViewController(controller) 

instead of:

let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)

Because pushViewController will add secondViewController to its stack. presentViewController was replacing your navigation controller that's why you couldn't see navigation bar.

Makalele
  • 7,431
  • 5
  • 54
  • 81
  • So Is there no way to enable navigation bar again? – Amsheer Nov 13 '15 at 09:40
  • Why enable? You don't disable it in the first place. You just hide it like any other ordinary view. So you always can unhide it. – Makalele Nov 13 '15 at 09:45
  • My other issue even I am not hiding my first navigation bar, navigation bar not visible in the second view controller. – Amsheer Nov 13 '15 at 10:54
  • Make sure you don't have second navigation view controller, but only one. – Makalele Nov 13 '15 at 11:04
  • I changed that too. Could you please check my edit 1. – Amsheer Nov 13 '15 at 11:09
  • Please check my Edit 2: Am i miss anything? – Amsheer Nov 13 '15 at 11:37
  • 1
    Yes, it won't work that way. navigationController.pushViewController should do the work. – Makalele Nov 13 '15 at 11:39
  • let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC") self.navigationController!.pushViewController(controller) – Makalele Nov 13 '15 at 11:43
  • fatal error: unexpectedly found nil while unwrapping an Optional value – Amsheer Nov 13 '15 at 11:55
  • Now I am using like this let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC"); self.navigationController!.pushViewController(controller, animated: false); Why is that error? – Amsheer Nov 13 '15 at 11:56
  • So you don't have a navigation controller at all (it's nil). Do you use storyboard. It should look like this: NAV containing VC. in VC you can access self.navigationController object. – Makalele Nov 13 '15 at 11:57
  • I have navigation controller for my initial view controller. – Amsheer Nov 13 '15 at 11:59
  • Please check my Edit 3 – Amsheer Nov 13 '15 at 12:01
  • Show your storyboard with navigation controller and view controller. – Makalele Nov 13 '15 at 12:12
  • Thanks it's working now. Please edit your answer with following code. Maybe that will help others. That is my mistake let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC") self.navigationController!.pushViewController(controller) – Amsheer Nov 13 '15 at 12:26
25
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)
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • I tried this not working. Could you please check my editted question. – Amsheer Nov 13 '15 at 09:41
  • Please check my Edit 2: Am i miss anything? – Amsheer Nov 13 '15 at 11:36
  • Thanks your logic is the perfect solution – Amsheer Nov 13 '15 at 12:27
  • Yes What i did wrong is I call new view controller like this presentViewController(secondViewController, animated: false, completion: nil). But I need to use this navigationController!.pushViewController(controller, animated: false);. @Makalele find the error. – Amsheer Nov 13 '15 at 12:31
  • @Amsheer -- sry I foget to ask this, present view controller does not show/embed with the navigation controller, if you want to perform this you need to manually embed with navigation controller . else you need to convert this into push viewcontroller – Anbu.Karthik Nov 13 '15 at 12:33
  • Thanks for the easiest solution. Works for me – Mo Zaatar Sep 14 '17 at 09:05
  • To support interactive pop gesture on navigation use with navigationController.setNavigationBarHidden(, animated: ) – evya Jul 09 '18 at 07:41
7

in viewDidLoad method of the view controller in which you don't want to show navigation bar add the line

navigationController.navigationBarHidden = true

you are presently hiding in all view controllers

Edit: You are presenting view controller instead it should be

self.navigationController!.pushViewController(controller) 
harsha yarabarla
  • 506
  • 4
  • 11
  • you can use Objc-Swift converter (https://objectivec2swift.com/#/converter/code) and post, your answer will be more helpful – swiftBoy Nov 13 '15 at 09:38
  • Please check my Edit 2: Am i miss anything? – Amsheer Nov 13 '15 at 11:37
  • Your second view controller is not assigned to a class see your screen shot it does not show any view controller class assigned. And also you are presenting view controller. instead you need to push view controller self.navigationController!.pushViewController(controller) – harsha yarabarla Nov 13 '15 at 11:56
  • btw are you sure your second view controller is inherited from UIViewController ? – harsha yarabarla Nov 13 '15 at 12:01
  • Yes everything is viewcontroller but my issue i call like presntviewcontroller. I need to call like pushviewcontroller.. – Amsheer Nov 13 '15 at 12:29
  • @harshayarabarla Please edit your answer with swift syntax. – Amsheer Nov 13 '15 at 12:32
6

do like in viewcontroller based hidden using Swift 4.0

To hide navigationController in viewWillAppear

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

To unhide navigationController in viewWillDisappear

override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    self.navigationController?.isNavigationBarHidden = false
}
Vittal Pai
  • 3,317
  • 25
  • 36
2

I am having same requirement in my swift project.

this is how I have handled Navigation bar

Make sure your first screen is embedded into Navigation controller

enter image description here

example we have two screens A and B

In screen A you need to hide navigation bar in viewWillAppear

override func viewWillAppear(animated: Bool)
    {
         super.viewWillAppear(animated)

        //hide navigation for screen A
        self.navigationController?.navigationBarHidden = true
    }

for enabling Navigation in screen B you need to add below code in screen 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)
        }

    }

Using above style, I can enable or disable navigation bar for specific screen, whenever I want

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
1

If you need to have this navigation bar hidden only in this controller, the best way is to show it in viewWillDisappear() and hide in viewWillAppear().

sunshinejr
  • 4,834
  • 2
  • 22
  • 32
0

It's too late to reply and there are other good answers but I would like to share what worked for me.

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVC")
    self.navigationController!.pushViewController(controller!, animated:true)
Ashfaque
  • 1,254
  • 1
  • 22
  • 38