简体   繁体   English

导航栏不显示

[英]Navigation bar doesn't show up

I have this problem: i have a view controller (embedded in a navigation controller) that after doing an action triggers a manual segue pushing a new view controller , however in the new view controller there is no navigation bar because in the first controller i had implemented the viewWillDisappear method like this: 我有这个问题:我有一个视图控制器 (嵌入在导航控制器中),在执行操作后触发手动segue推新视图控制器 ,但是在新视图控制器中没有导航栏,因为在第一个控制器中我有实现了viewWillDisappear方法,如下所示:

StartViewController StartViewController

- (void)viewWillDisappear:(BOOL)animated {
  // Hide the navigation bar just before the view disappear
  [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

Here is the code for the manual segue that's inside an IBAction : 以下是IBAction中手动segue的代码:

[self performSegueWithIdentifier:@"tutorialSegue" sender:self];

DestinationViewController DestinationViewController

I'd tried like this 我试过这样的

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
}

but it doesn't work, actually in the debugger i noticed navigationcontroller is equal to nil and i just can't figured out why. 但它不起作用,实际上在调试器中我注意到navigationcontroller等于nil ,我只是想不通原因。

If you want StartViewController to hide navigation bar, and DestinationViewController to show it: Add corresponding code to -(void)viewWillAppear: method. 如果你想让StartViewController隐藏导航栏,并使用DestinationViewController来显示它:将相应的代码添加到-(void)viewWillAppear:方法。

StartViewController: StartViewController:

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

DestinationViewController: DestinationViewController:

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

If you want both view controllers to have navigation bar, just remove all lines that contain setNavigationBarHidden: 如果您希望两个视图控制器都有导航栏,只需删除包含setNavigationBarHidden:所有行setNavigationBarHidden:

You problem here is that your viewDidLoad is being called before your viewWillDisappear . 这里的问题是你的viewDidLoad在你的viewWillDisappear之前被调用。 You must load a new view before you can unload the parent (visually). 您必须先加载新视图,然后才能卸载父视图(可视)。 So you are setting the nav bar visible and hiding it again. 因此,您可以将导航栏设置为可见并再次隐藏它。

Navigation bars are universal between the views nested inside of it. 导航栏在嵌套在其中的视图之间是通用的。 There really should be no reason to hide it when a view is disappearing unless the childview view does not need it. 除非子视图视图不需要,否则在视图消失时确实没有理由隐藏它。 If you further explained what you are attempting to do we can help more. 如果您进一步解释了您尝试做的事情,我们可以提供更多帮助。 But in the mean time if you just remove your viewWillDisappear implementation (at least what you are showing us) you should be good. 但同时如果你只是删除你的viewWillDisappear实现(至少你向我们展示的那样)你应该是好的。 Otherwise you can set the hidden property to no in your DestinationViewController's viewWillAppear or viewDidAppear (depending on the calling order). 否则,您可以在DestinationViewController的viewWillAppearviewDidAppear设置hidden属性为no (取决于调用顺序)。

Just another angle on this: When in the storyboard editor, I reset the initial view controller by accident (in the properties pane), as a result I lost the navigation bar. 另一个角度:在故事板编辑器中,我意外地重置了初始视图控制器(在属性窗格中),结果我丢失了导航栏。 Simply resetting the Navigation Controller as "Is Initial View Controller" brought it back. 只需将导航控制器重置为“是初始视图控制器”即可将其恢复。

If your code is looks like 如果您的代码看起来像

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

change it to, 改成它,

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

type "super viewWillappear..." before "self.nav..." 在“self.nav ...”之前键入“super viewWillappear ...”

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

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