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;
}
Give my credit to @chad-m 's answer.
Here is the Swift version:
MyNavigationController.swift
Difference between chad-m's answer and mine:
Inherit from UINavigationController, so you won't pollute your rootViewController.
use
self.viewControllers.first
rather thanhomeViewController
, so you won't do this 100 times for your 100 UINavigationControllers in 1 StoryBoard.Another approach I found is to set a delegate for the
NavigationController
:and use
setNavigationBarHidden
innavigationController:willShowViewController:animated:
Easy way to customize the behavior for each
ViewController
all in one place.In case anyone still having trouble with the fast backswipe cancelled bug as @fabb commented in the accepted answer.
I manage to fix this by overriding
viewDidLayoutSubviews
, in addition toviewWillAppear/viewWillDisappear
as shown below:In my case, I notice that it is because the root view controller (where nav is hidden) and the pushed view controller (nav is shown) has different status bar styles (e.g. dark and light). The moment you start the backswipe to pop the view controller, there will be additional status bar colour animation. If you release your finger in order to cancel the interactive pop, while the status bar animation is not finished, the navigation bar is forever gone!
However, this bug doesn't occur if status bar styles of both view controllers are the same.
The simplest implementation may be to just have each view controller specify whether its navigation bar is hidden or not in its
viewWillAppear:animated:
method. The same approach works well for hiding/showing the toolbar as well:Hiding navigation bar only on first page can be achieved through storyboard as well. On storyboard, goto Navigation Controller Scene->Navigation Bar. And select 'Hidden' property from the Attributes inspector. This will hide navigation bar starting from first viewcontroller until its made visible for the required viewcontroller.
Navigation bar can be set back to visible in ViewController's ViewWillAppear callback.
If what you want is to hide the navigation bar completely in the controller, a much cleaner solution is to, in the root controller, have something like:
When you push a child view in the controller, the Navigation Bar will remain hidden; if you want to display it just in the child, you'll add the code for displaying
it(self.navigationController.navigationBarHidden=NO;)
in theviewWillAppear
callback, and similarly the code for hiding it onviewWillDisappear