UITabBarController选中的项目显示导航栏

时间:2017-03-21 11:18:07

标签: ios objective-c uitabbarcontroller uinavigationbar

我正在使用UITabBarController和UINavigationController制作应用程序。 视图控制器图如下所示:

TabBar Controller > Navigation Controller > View Controller

在第一个视图控制器处,我将导航栏设置为隐藏。但是当我点击TabBar项目以显示其他视图控制器然后单击回到第一个视图控制器时,显示了导航栏。每当我添加到所有其他视图控制器中时:

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

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

任何人都知道如何在第一个视图控制器中设置导航栏以始终隐藏?请帮忙。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

If you have multiple tabs at bottom who follows structure:

Controller > Navigation Controller > View Controller

Then to hide navigation bar in first view controller, you need to use following code 

// TO HIDE NAVIGATION BAR in first view controller

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

// TO SHOW NAVIGATION BAR in other view controllers 

If you want all other views should have navigation bar visible, you could use viewWillDisappear to set it to visible again.


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

答案 1 :(得分:1)

如果您在某些视图中,控制器必须使用此方法:

[self.navigationController setNavigationBarHidden:YES animated:YES];

隐藏任何viewController中的导航栏:

<强>目标C

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];   // Will hides Navigationbar  
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated]; // Will shows Navigationbar
    [super viewWillDisappear:animated];
}

<强>夫特

override func viewWillAppear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
    super.viewWillAppear(animated)
}

override func viewWillDisappear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
    super.viewWillDisappear(animated)
}