Hide navigation bar, but when I transition to the

2019-04-18 08:39发布

问题:

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:

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.

回答1:

Hey why don't you use navigation bar as a UIToolbar.

Instead Of hiding UINavigation you can mimic navigation controller to UITootlbar by adding buttons to it.

hiding unhiding UINavigation would be complex.

I am Uploaded the dropbox link.



回答2:

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.



回答3:

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

self.navigationController.navigationBar.frame = CGRectMake(0, 0, 0, 0);

For unhiding the navigation bar set the frame in viewWillDisappear:

self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);


回答4:

Well, as I understand from your question the only problem of your current approach is temporary appearance of original "Back" button. So why not just block this button for the view controller?

self.navigationItem.hidesBackButton = YES;

I think it might help. But for your main task I'd rather suggest you to use custom navigation bar with ability to add as many buttons as you want. Such approach is more natural for iOS and you'll probably never face the issues like you mentioned in your question.



回答5:

Hello The following stuff done my job

In FirstViewController put this method

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

and in SecondViewController put this method

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

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

I have not used viewWillDisappear for my work done.

Hope will help you.



回答6:

It could be as simple as moving your code from viewWillDisappear to viewDidDisappear.

Also, avoid using animations in any view*appear methods, as the view-transition is already being animated.



回答7:

MY problem is that when I push a viewController with a navigationBar onto one without it by using a customised transition moving it from the left side, I am not able to have it follow the second view controller's frame. It always drops from above producing a very unpleasant effect whenever I perform this action.



回答8:

You can show the navigation bar, which makes it appear in your parent view controller, and then transition the alpha from 0 to 1:

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

    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 0;

    [UIView animateWithDuration:0.3 animations:^{
        self.navigationController.navigationBar.alpha = 1;
    }];
}