How to hide navigation bar in iPhone?

后端 未结 7 541
悲&欢浪女
悲&欢浪女 2021-01-16 04:55

Currently i am working in iPhone app, I have two screen like A and B, A has no navigation bar, but B has navigation bar. so i set like this.

Class A:



        
7条回答
  •  北海茫月
    2021-01-16 05:08

    I've had to solve this recently and I found that it was necessary to call setNavigationBarHidden:NO immediately after pushViewController: and setNavigationBarHidden:YES immediately after popViewController:, with animated YES in each call.

    So, when pushing:

    [nc pushViewController:classBView animated:YES]
    [nc setNavigationBarHidden:NO animated:YES]
    

    and when popping:

    [nc popViewControllerAnimated:YES]
    [nc setNavigationBarHidden:YES animated:YES]
    

    But in my case, while I could do pushing as above, I didn't want to alter my class B and instead wanted it to not know of care that the navigation bar wasn't previously hidden (since its not my code). Also, that view gets popped using the normal Back button, there was no explicit call to popViewControllerAnimated:. What was going to work best in my code was to make my class A be the UINavigationController delegate and hide the toolbar on a delegate method call when the pop occurs.

    Unfortunately I found that the UINavigationControllerDelegate methods weren't too helpful, willShowViewController & didShowViewController are called indistinguishably when pushing my class B view or when popping back to it from another one that it has pushed.

    I followed a suggestion in https://stackoverflow.com/questions/642312/ about overriding UINavigationController and I made some custom delegate methods, one is called right after [super popViewControllerAnimated:]. My subclass is available at https://gist.github.com/jpmhouston/6118713 and delegate method is:

    - (void)navigationController:(UINavigationController *)navigationController isPoppingViewController:(UIViewController *)poppedViewController backTo:(UIViewController *)revealedViewController {
        if (revealedViewController == self && [poppedViewController isKindOfClass:[MyClassB class]]) {
            [navigationController setNavigationBarHidden:YES animated:YES];
            // ...and more code to run only when going from class B back to class A
        }
    }
    

    I'm sure there are simpler ways to have setNavigationBarHidden: called following the Back button being pressed, but this worked for me.

提交回复
热议问题