简体   繁体   English

如何在iPhone中隐藏导航栏?

[英]How to hide navigation bar in iPhone?

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. 目前我在iPhone应用程序工作,我有两个屏幕,如A和B,A没有导航栏,但B有导航栏。 so i set like this. 所以我这样设置。

Class A: A类:

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

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

Class B: 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. 然后我运行应用程序,当我去B类到A的前一课时,蓝色显示在B类以下附加屏幕截图供您参考。 How to fix this issue? 如何解决这个问题? please help me 请帮我

Thanks in Advance 提前致谢

在此输入图像描述

Set it in class B 在B类中设置它

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

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. in - (void)viewWillAppear:(BOOL)动画或稍后在两个类的视图生命周期中。 [Avoid doing this in - (void)viewDidLoad .] [避免在- (void)viewDidLoad中执行此操作。]

The trick here is in using the setNavigationBarHidden: animated : method (in place of the simpler setNavigationBarHidden: method). 这里的技巧是使用setNavigationBarHidden: animated方法(代替更简单的setNavigationBarHidden:方法)。 This will ensure your UI issue goes away and also any positional issues due to it. 这将确保您的UI问题消失,以及由此引起的任何位置问题。

PS Check the value of self.navigationController. PS检查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. navigationBarHidden(而不是self.navigationController。navigationBar.hidden)如果您需要检查,如果您的导航栏是隐藏的,在某些时候,在你的代码。

Try setting the navigationBarHidden: in viewWillDisAppear of class B 尝试在B类的 viewWillDisAppear中设置navigationBarHidden: .

in class B B班

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

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. 简单的解决方案 - 隐藏UINavigationController的动画栏,如果你想在某个控制器上显示它,只需添加一个UINavigationBar即可。

Use below line to hide navigationBar in viewWillAppear: method - 使用下面的行来隐藏viewWillAppear: navigationBar viewWillAppear:方法 -

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

Your code is okay for Hide and Unhide the navigationBar . 您的代码可以HideUnhide 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. 问题是你在viewWillAppear:隐藏了Class AnavigationBar viewWillAppear:在出现视图之前调用它,所以在加载Class A视图之前,每次都会隐藏navigationBar
And if we talk about your blue color i think it is your window color. 如果我们谈论你的蓝色,我认为这是你的window颜色。 Because after hidden the navigationBar there will be a space above your self.view which height is 44.0 . 因为在隐藏了navigationBar之后,你的self.view上方会有一个空格,高度为44.0 So there are three options to fixed it. 所以有三种方法可以修复它。

  • Hide Class A 's navigationBar in Class A 's viewDidAppear: method. Class AviewDidAppear:方法中隐藏Class AnavigationBar
  • Set your window color what you want to show. 设置要显示的window颜色。
  • 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. 您可以添加到图像window的背景中,在图像上做出navigationBar相同的Class BnavigationBar ,所以当原来navigationBar会被隐藏起来会看到。

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. 我最近必须解决这个问题,我发现有必要调用setNavigationBarHidden:NOpushViewController:之后立即setNavigationBarHidden:NO pushViewController:并且在popViewController:之后立即执行setNavigationBarHidden:YES ,并在每个调用中使用动画YES。

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). 但在我的情况下,虽然我可以按上述方式进行推送,但我不想改变我的B类 ,而是希望它不知道以前没有隐藏导航栏(因为它不是我的代码)。 Also, that view gets popped using the normal Back button, there was no explicit call to popViewControllerAnimated: . 此外,该视图使用普通的Back按钮弹出,没有显式调用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. 在我的代码中最有效的方法是让我的类A成为UINavigationController委托,并在弹出窗口时隐藏委托方法调用上的工具栏。

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. 不幸的是,我发现UINavigationControllerDelegate方法没有太大的帮助,当推送我的B类视图或从它推动的另一个视图didShowViewController回到它时, willShowViewControllerdidShowViewController被无法区分地调用。

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:] . 我按照https://stackoverflow.com/questions/642312/中有关覆盖UINavigationController的建议,我做了一些自定义委托方法,一个在[super popViewControllerAnimated:]之后调用。 My subclass is available at https://gist.github.com/jpmhouston/6118713 and delegate method is: 我的子类可以在https://gist.github.com/jpmhouston/6118713获得 ,委托方法是:

- (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. 我确信有更简单的方法可以使用setNavigationBarHidden:按下后退按钮后调用,但这对我setNavigationBarHidden:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM