如何从一个特定的视图控制器隐藏导航栏

kin*_*ton 67 cocoa-touch ios

我创建了一个两个闪屏的iPhone应用程序.之后用户被带到第一个视图.我添加了一个UINavigationController.它工作得很好.

如何单独删除打开视图的导航栏?

主窗口

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


self.splashScreen = [[SplashScreen alloc] 
                initWithNibName:@"SplashScreen" 
                bundle:nil];
if (self.pageController == nil) {
openingpage *page=[[openingpage alloc]initWithNibName:@"openingpage" bundle:[NSBundle mainBundle]];
    self.pageController = page;
    [page release];
}
[self.navigationController pushViewController:self.pageController animated:YES];

[window addSubview:splashScreen.view];

 [splashScreen displayScreen];
[self.window makeKeyAndVisible];

return YES;
 }
Run Code Online (Sandbox Code Playgroud)

ber*_*ium 145

所以,如果你在某些视图中,控制器必须使用这种方法:

// swift
self.navigationController?.setNavigationBarHidden(true, animated: true)

// objective-c
[self.navigationController setNavigationBarHidden:YES animated:YES]; 
Run Code Online (Sandbox Code Playgroud)

更多说明:

UINavigationController有一个属性navigationBarHidden,它允许您隐藏/显示整个导航控制器的导航栏.

让我们在下一个等级中掠夺:

--UINavigationController
------UIViewController1
------UIViewController2
------UIViewController3
Run Code Online (Sandbox Code Playgroud)

三个UIViewController中的每一个都有导航栏,因为它们位于UINavigationController中.例如,你想将bar隐藏到第二个(实际上它与哪一个无关),然后写入UIViewController2:

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES]; // it shows the bar back
}
Run Code Online (Sandbox Code Playgroud)

  • 还有` - (void)setNavigationBarHidden:(BOOL)隐藏动画:(BOOL)动画`方法,它显示/隐藏带有视图动画的导航栏.这个答案引领了我.谢谢. (4认同)
  • 我想要的是只隐藏第一个"打开视图"的导航栏.使用上面隐藏与第一个视图控制器相关的文件的整个导航栏 (2认同)

小智 17

斯威夫特4:

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

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    navigationController?.setNavigationBarHidden(false, animated: false)
}
Run Code Online (Sandbox Code Playgroud)


小智 6

这对我有用:

斯威夫特 4

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

//reappears navigation bar on next page
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.setNavigationBarHidden(false, animated: true)
}
Run Code Online (Sandbox Code Playgroud)