ios Storyboard - 以编程方式隐藏导航控制器的顶部栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8215533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storyboard - Hiding top bar of navigation controller programmatically
提问by animal_chin
I'm using a storyboard and I'm trying to hide a top bar of my main navigation controller when a certain button is pressed (or function is called). I know I have to initialize an object referring to a navigation controller from a storyboard (using identifiers), but when I send the setNavigationBarHidden message to this newly created object nothing really happens on screen.
我正在使用故事板,当按下某个按钮(或调用函数)时,我试图隐藏主导航控制器的顶部栏。我知道我必须从情节提要中初始化一个引用导航控制器的对象(使用标识符),但是当我将 setNavigationBarHidden 消息发送到这个新创建的对象时,屏幕上并没有真正发生任何事情。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UINavigationController *navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:@"MyNavController"];
[navController setNavigationBarHidden:YES animated:YES];
Does anyone know what the problem is?
有谁知道问题是什么?
回答by animal_chin
Finally solved it. You should always hide navigation bar only through viewController.
终于解决了。您应该始终仅通过 viewController 隐藏导航栏。
In my question above I instantiated a whole new navigationController which didn't point at the real navController on the screen. You can obtain the "real" one through the view controller like this:
在我上面的问题中,我实例化了一个全新的 navigationController,它没有指向屏幕上真正的 navController。您可以像这样通过视图控制器获得“真实”的:
[viewController.navigationController setNavigationBarHidden:YES animated:YES];
回答by Aamir
There are 2 ways to hide top bar of Navigation Controller:
有两种方法可以隐藏导航控制器的顶部栏:
1) Programatically
1) 以编程方式
[self.navigationController setNavigationBarHidden:YES animated:YES];
2) Using Interface Builder
2) 使用界面生成器
You can uncheck "Shows Navigation Bar" in property list of Navigation Controller.
您可以在导航控制器的属性列表中取消选中“显示导航栏”。
Please make sure you have selected scene which contain Navigation Controller. Example image of scene.
回答by Brian
in swift 4:
快速 4:
self.navigationController!.navigationBar.isHidden = true
something like this:
像这样:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController!.navigationBar.isHidden = true
}
回答by jwall
I faced same problem, this worked for me
我遇到了同样的问题,这对我有用
navController.navigationBar.hidden = YES;
navController.navigationBar.hidden = YES;
回答by user1296082
The answers are correct. Just want to add that you probably want to put back the navigation bar when leaving the view as other views might use it and when you hide for one its hidden for all.
答案是正确的。只想补充一点,您可能希望在离开视图时放回导航栏,因为其他视图可能会使用它,而当您为一个视图隐藏时,它会为所有视图隐藏。
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
}
and before you leave the view:
在离开视图之前:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.isHidden = false
}
`
`
回答by Durai Amuthan.H
In Xamarin.iOS
在 Xamarin.iOS 中
base.NavigationController.NavigationBarHidden = true;
回答by Avijit Nagare
回答by Dhiru
SetRoot RootViewController after Changing the Properties.
更改属性后设置Root RootViewController。
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// Create Navigation controller with RootViewController
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@"MyRootViewController"]];
Setting RootViewController
设置 RootViewController
[appDelegate.window setRootViewController: navController];
or else you Can Do in any ViewController
否则你可以在任何 ViewController 中做
[self.navigationController setNavigationBarHidden:YES animated:YES];