Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 303
Doug Smith Avatar asked Apr 28 '13 21:04

Doug Smith


People also ask

How do I get rid of the back button on my navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


1 Answers

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.

like image 109
matt Avatar answered Sep 21 '22 04:09

matt