I have the code below that hides and shows the navigational bar. It is hidden when the first view loads and then hidden when the "children" get called. Trouble is that I cannot find the event/action to trigger it to hide again when they get back to the root view....
I have a "test" button on the root page that manually does the action but it is not pretty and I want it to be automatic.
-(void)hideBar
{
self.navController.navigationBarHidden = YES;
}
-(void)showBar
{
self.navController.navigationBarHidden = NO;
}
One slight tweak I had to make on the other answers is to only unhide the bar in viewWillDisappear if the reason it is disappearing is due to a navigation item being pushed on it. This is because the view can disappear for other reasons.
So I only unhide the bar if this view is no longer the topmost view:
The nicest solution I have found is to do the following in the first view controller.
Objective-C
Swift
This will cause the navigation bar to animate in from the left (together with the next view) when you push the next
UIViewController
on the stack, and animate away to the left (together with the old view), when you press the back button on theUINavigationBar
.Please note also that these are not delegate methods, you are overriding
UIViewController
's implementation of these methods, and according to the documentation you must call the super's implementation somewhere in your implementation.The currently accepted answer does not match the intended behavior described in the question. The question asks for the navigation bar to be hidden on the root view controller, but visible everywhere else, but the accepted answer hides the navigation bar on a particular view controller. What happens when another instance of the first view controller is pushed onto the stack? It will hide the navigation bar even though we are not looking at the root view controller.
Instead, @Chad M.'s strategy of using the
UINavigationControllerDelegate
is a good one, and here is a more complete solution. Steps:UINavigationController
-navigationController:willShowViewController:animated
method to show or hide the navigation bar based on whether it is showing the root view controllerComplete code for this solution can be found in this Gist. Here's the
navigationController:willShowViewController:animated
implementation:By implement this code in your ViewController you can get this effect Actually the trick is , hide the navigationBar when that Controller is launched
and unhide the navigation bar when user leave that page do this is viewWillDisappear
After multiple trials here is how I got it working for what I wanted. This is what I was trying. - I have a view with a image. and I wanted to have the image go full screen. - I have a navigation controller with a tabBar too. So i need to hide that too. - Also, my main requirement was not just hiding, but having a fading effect too while showing and hiding.
This is how I got it working.
Step 1 - I have a image and user taps on that image once. I capture that gesture and push it into the new
imageViewController
, its in theimageViewController
, I want to have full screen image.Step 2 - All these steps below are in the ImageViewController
Step 2.1 - In ViewDidLoad, show the navBar
Step 2.2 - In
viewDidAppear
, set up a timer task with delay ( I have it set for 1 sec delay). And after the delay, add fading effect. I am using alpha to use fading.step 2.3 - Under
viewWillAppear
, add singleTap gesture to the image and make the navBar translucent.Step 3 - Finally in
viewWillDisappear
, make sure to put all the stuff backI would put the code in the viewWillAppear delegate on each view being shown:
Like this where you need to hide it:
Like this where you need to show it: