如何在第一次查看时隐藏 UINavigationBar

发布于 2024-12-01 07:56:23 字数 526 浏览 0 评论 0原文

我想隐藏仅在第一个视图中自动加载导航项目的 UINavigationBar,并且想知道如何才能完成这项工作。

我尝试这样做

//RootViewController.m

#import "mydelegate.h" //-- this is where the view controller is initialized

//...

- (void)viewDidLoad
{
    [super viewDidLoad];
    navigationController *navController = [[navigationController alloc] init];
    [navigationController setNavigationBarHidden:YES animated:animated];
}

//.....

但是我收到错误,因为我想我没有正确地从委托文件中调用 navigationController 或者这不可能像来自另一个类的方法那样调用它。

任何帮助将不胜感激。

I am wanting to hide the UINavigationBar that automatically loads with the Navigation project for the first view only and am wondering how I can make this work.

I have tries to do it like this

//RootViewController.m

#import "mydelegate.h" //-- this is where the view controller is initialized

//...

- (void)viewDidLoad
{
    [super viewDidLoad];
    navigationController *navController = [[navigationController alloc] init];
    [navigationController setNavigationBarHidden:YES animated:animated];
}

//.....

However I am getting errors because I guess I am not calling navigationController across from the delegate file properly or this is just not possible to call it like you would a method from another class.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白昼 2024-12-08 07:56:23

这里有一些问题。

  1. 您应该使用 self.navigationController 访问呈现视图控制器的导航控制器。您的代码片段正在创建一个新的 UINavigationController;隐藏那个酒吧不会给你带来任何好处。
  2. 您不应将导航栏隐藏在 viewDidLoad 中,而应将其隐藏在 viewWillAppear: 中。您可以在 viewDidLoad 中隐藏导航栏,并且当视图最初呈现时导航栏将被隐藏,但是如果您要推送另一个呈现导航栏的视图并点击后退按钮,导航栏将保持可见。

您的 viewWillAppear: 应该看起来像这样:

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

并且您在此导航控制器上推送的其他视图控制器中的 viewWillAppear: 方法应该适当地显示或隐藏导航栏。

There are a couple things wrong here.

  1. You should be accessing the navigation controller that's presenting your view controller by using self.navigationController. Your code snippet is creating a new UINavigationController; hiding that bar won't get you anything.
  2. Instead of hiding the navigation bar in viewDidLoad, you should hide it in viewWillAppear:. You can hide the navigation bar in viewDidLoad, and the navigation bar will be hidden when the view is initially presented, but if you were to push another view that presented the navigation bar and hit the back button, the navigation bar would remain visible.

Your viewWillAppear: should look something like this:

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

And the viewWillAppear: methods in other view controllers you push on this navigation controller should show or hide the navigation bar appropriately.

轻拂→两袖风尘 2024-12-08 07:56:23

您是否正在访问 UINavigationController 的正确实例?
您可以通过 self.navigationController 从已添加到其堆栈的任何 UIViewController 访问 UINavigationController。

否则,也许这会有所帮助:
iPhone 仅在首页隐藏导航栏

Are you accessing the correct instance of the UINavicationController?
You can access the UINavigationController via self.navigationController from any UIViewController that's been added to its stack.

Otherwise, maybe this'll help:
iPhone hide Navigation Bar only on first page

辞别 2024-12-08 07:56:23

iPhone 仅在首页隐藏导航栏 尝试这个答案

。它为我解决了问题。

我也遇到了导航栏的问题。我可以让它消失,但我无法让它在需要的时候重新出现。此链接解释了如何解决此问题,只需在 viewWillAppear 中打开它,并在 viewWillDisappear 中关闭它即可。

iPhone hide Navigation Bar only on first page

Try this answer. It solved the problem for me.

I was having the problem with the navigation bar as well. I could make it disappear, but I couldn't make it reappear when it needed to. This link explains how you can solve this problem, simply by turning it on in viewWillAppear, and off in viewWillDisappear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文