views:

9994

answers:

3

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;
}
+8  A: 

I would put the code in the viewWillAppear delegate on each view being shown:

Like this where you need to hide it:

- (void)viewWillAppear:(BOOL)animated
{
        [yourObject hideBar];
}

Like this where you need to show it:

- (void)viewWillAppear:(BOOL)animated
{
        [yourObject showBar];
}
Pablo Santa Cruz
Thanks Pablo, I had it on the wrong view!! :o)
Lee Armstrong
No prob. Good luck! iPhone programming is fuuuuun!
Pablo Santa Cruz
Lee, if this fixed your problem, mark Pablo's as the "solution" answer.
Roger Nolan
The only problem with this is that the navigation bar "pops" out and into view as you navigate from one view to the next. Is it possible just to have the navigation bar not there on the first view, and when the second view slides into place, it has the nav bar, without any popping?
@henningTo make the NavBar slide in/out as you would expect, you need to use setNavigationBarHidden:animated:. See Alan Rogers' answer below (which should really be marked as the "solution").
Nick Forge
This answer is slightly wrong (viewWill/DidAppear) should be calling super. Also See my answer below for a solution where you don't need to add it to EVERY view controller.
Alan Rogers
Thanks Alan for pointing this out.
Pablo Santa Cruz
+1  A: 

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:

@implementation MainViewController
- (void)viewDidLoad {
    self.navigationController.navigationBarHidden=YES;
    //...extra code on view load  
}

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 the viewWillAppear callback, and similarly the code for hiding it on viewWillDisappear

Alex B
+25  A: 

The nicest solution I have found is to do the following in the first view controller.

- (void) viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

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 the NavigationBar.

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.

Alan Rogers
Very nice answer my friend
ing0
That is a fantastic snippet of code.
kim3er