How to hide navigation bar in iPhone?

后端 未结 7 542
悲&欢浪女
悲&欢浪女 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:04

    You'll need to use this code:

    [navigationController setNavigationBarHidden: YES animated:YES]

    in - (void)viewWillAppear:(BOOL)animated or later in the view lifecycle in both classes. [Avoid doing this in - (void)viewDidLoad.]

    The trick here is in using the setNavigationBarHidden:animated: method (in place of the simpler setNavigationBarHidden: method). This will ensure your UI issue goes away and also any positional issues due to it.

    P.S. Check the value of self.navigationController.navigationBarHidden (instead of self.navigationController.navigationBar.hidden) if you need to check if your navigation bar is hidden, at some point, in your code.

    0 讨论(0)
  • 2021-01-16 05:05

    I don't think a behavior when you are hiding and showing the navigation bar dynamically as you are pushing controllers is supported.

    Simple solution - hide the animation bar of the UINavigationController and if you want to show it on some controller, just add a UINavigationBar to it.

    0 讨论(0)
  • 2021-01-16 05:07

    Use below line to hide navigationBarin viewWillAppear: method -

    -(void)viewWillAppear:(BOOL)animated
    {
        self.navigationController.navigationBar.hidden=YES;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-16 05:12

    Set it in class B

    -(void)viewWillAppear:(BOOL)animated
    {
     [self.navigationController setNavigationBarHidden:NO];
    }
    
    0 讨论(0)
  • 2021-01-16 05:19

    Your code is okay for Hide and Unhide the navigationBar. The problem is that you're hiding Class A's navigationBar in viewWillAppear: which is called just before appearing the view so before loading the Class A view navigationBar is being hidden each time.
    And if we talk about your blue color i think it is your window color. Because after hidden the navigationBar there will be a space above your self.view which height is 44.0. So there are three options to fixed it.

    • Hide Class A's navigationBar in Class A's viewDidAppear: method.
    • Set your window color what you want to show.
    • You can add an image to window background in which at top of image make a navigationBar same as Class B's navigationBar so when the original navigationBar will be hide it will see.
    0 讨论(0)
提交回复
热议问题