无法在 xcode 中隐藏导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18471253/
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
Unable to hide navigation bar in xcode
提问by heyred
I have made a simple app using the Storyboard editor in xcode 4.6.3. The first view is a navigation controller with some simple buttons for navigation. This then by default adds the navigation bar to the top of each new view I create when I connect the buttons to each of their pages.
我使用 xcode 4.6.3 中的 Storyboard 编辑器制作了一个简单的应用程序。第一个视图是带有一些简单导航按钮的导航控制器。然后默认情况下,当我将按钮连接到每个页面时,将导航栏添加到我创建的每个新视图的顶部。
However, I want the first page (landing page I guess I would call it) to have no top bar. I follow the instructions hereon how to disable the top navigation bar in Storyboard mode. However, this then disables all navigation bars for all views linked to this main view.
但是,我希望第一页(我想我会称之为着陆页)没有顶部栏。我按照此处有关如何在故事板模式下禁用顶部导航栏的说明进行操作。但是,这会禁用链接到此主视图的所有视图的所有导航栏。
I also change the colour of sub pages' top navigation bars but this does not work either. I run the application on the emulator but the changes dont seem to take affect.
我还更改了子页面顶部导航栏的颜色,但这也不起作用。我在模拟器上运行应用程序,但更改似乎没有生效。
Can anyone please advise? I am new to objective c (experience in Java mostly) and would like to get an app out quickly. My problem is time and Storyboard seems to have solved this as I can get something together fairly quickly.
任何人都可以请指教吗?我是objective c 的新手(主要是Java 经验)并且想快速推出一个应用程序。我的问题是时间和 Storyboard 似乎已经解决了这个问题,因为我可以相当快地把一些东西放在一起。
回答by Adam21e
I just fired up an app and had the same issue, the line you are looking for is:
我刚刚启动了一个应用程序并遇到了同样的问题,您正在寻找的行是:
self.navigationController.navigationBar.hidden = YES;
Full Code is:
完整代码是:
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = YES;
}
Make sure you turn it back on with the next controller:
确保使用下一个控制器重新打开它:
self.navigationController.navigationBar.hidden = NO;
Was only tested in a later version of Xcode but should work fine for 4.6.3
仅在更高版本的 Xcode 中进行过测试,但对于 4.6.3 应该可以正常工作
(edit to change from viewDidLoad to viewWillAppear)
(编辑以从 viewDidLoad 更改为 viewWillAppear)
回答by Radu Ursache
in Swift you can use the almost obvious
在 Swift 中,您可以使用几乎显而易见的
self.navigationController?.navigationBar.isHidden = true;
and
和
self.navigationController?.navigationBar.isHidden = false;
to show or hide the navigation bar. make sure you allow the view to load so call those in viewWillAppear or viewDidAppear.
显示或隐藏导航栏。确保您允许加载视图,以便在 viewWillAppear 或 viewDidAppear 中调用它们。
回答by Dan Korkelia
This might be an old post but seems still relevant. I ran into this issue and thought this might be useful to update to Swift Version 4.
这可能是一个旧帖子,但似乎仍然相关。我遇到了这个问题,并认为这可能对更新到 Swift 版本 4 有用。
Swift 4
斯威夫特 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
For example, you would insert it into initial view controller where you don't want to see navigation bar. This will allow it to hide navigation bar. And animate it into view on next segue.
例如,您可以将它插入到您不想看到导航栏的初始视图控制器中。这将允许它隐藏导航栏。并在下一个转场中将其动画化。
One thing to keep in mind, because it animates into view you should make sure your constraints are not aligned to save area which includes navigation bar but rather to superview.
要记住的一件事,因为它动画进入视图,你应该确保你的约束没有与包含导航栏的保存区域对齐,而是与超级视图对齐。
Hoping this is helpful.
希望这是有帮助的。
回答by Ernest
The answer below is correct and is relevant for Swift 4. Another posts use viewWillDisappear to show the navigation bar again instead of viewDidDisappear, but this does not work. For everyone with this problem in swift 4 DO NOT USE:
下面的答案是正确的并且与 Swift 4 相关。另一个帖子使用 viewWillDisappear 来再次显示导航栏而不是 viewDidDisappear,但这不起作用。对于在 swift 4 中遇到此问题的每个人,请勿使用:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
To show the navigation bar again use:
要再次显示导航栏,请使用:
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}