Ios – Hide navigation bar, but when I transition to the previous view (popped) it shows the old back button temporarily. Why

iosobjective cuinavigationbaruinavigationcontrolleruistoryboardsegue

I have view controllers in a navigation controller (root: RootViewController, second: ReadingViewController), but in the second view controller I want to disable the navigation bar for a UIToolBar (as I don't need the title and want more buttons, like in iBooks or the Facebook app). Problem is, when I hide the navigation bar in the second view, it appears randomly for a second again when I pop the view controller (go back).

When I pop the view controller the back button appears for a second:

enter image description here

In the second view controller I hide the nav bar in viewWillAppear::

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

Also in the second view controller, I restore the nav bar in viewWillDisappear::

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    // ... other stuff

    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

I'm wondering how I combat this issue so that the view controllers transition seamlessly.

Best Answer

The problem here is that viewDidLoad is way too soon! Remember, viewDidLoad does not have anything to do with the interface and the actual push animation. It does not mean that this view controller's view is about to appear on screen! It merely means that the view controller has obtained its view.

I made a video, showing what happens on my machine as I move back and forth between two view controllers in a navigation interface, one of which shows the navigation bar, the other does not: http://youtu.be/PxpchytWQ4A

To me, that's as coherent as you are going to get when showing and hiding the nav bar as you push and pop! Here's the code I used. The view controller that hides its nav bar is of class ViewController2. This code is in the app delegate:

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    dispatch_async(dispatch_get_main_queue(), ^{
        [(UINavigationController*)self.window.rootViewController setDelegate:self];
    });
    return YES;
}

-(void)navigationController:(UINavigationController *)nc 
     willShowViewController:(UIViewController *)vc 
                   animated:(BOOL)animated 
{
    [nc setNavigationBarHidden:([vc isKindOfClass:[ViewController2 class]]) 
                      animated:animated];   
}

That's all I did.

Related Topic