How to hide navigation bar in iPhone?

2019-02-20 20:13发布

问题:

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:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"A";
    [self.navigationController setNavigationBarHidden:YES];
}

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

Class B:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"B";
    [self.navigationController setNavigationBarHidden:NO];
}

-(void)Previousscreen
{
  [self.navigationController popViewControllerAnimated:YES];
}

then i run the application, When i go to previous class like B to A at the time blue color show in B class below attached screen shot for your reference. How to fix this issue? please help me

Thanks in Advance

回答1:

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.



回答2:

Set it in class B

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


回答3:

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.



回答4:

Use below line to hide navigationBarin viewWillAppear: method -

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
}


回答5:

Try setting the navigationBarHidden: in viewWillDisAppear of class B

in class B

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


回答6:

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.


回答7:

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.