iPhone仅在第一页隐藏导航栏 [英] iPhone hide Navigation Bar only on first page

查看:31
本文介绍了iPhone仅在第一页隐藏导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码可以隐藏和显示导航栏.它在第一个视图加载时隐藏,然后在孩子"被调用时隐藏.问题是当他们回到根视图时,我找不到触发它再次隐藏的事件/动作......

I have the code below that hides and shows the navigational bar. It is hidden when the first view loads and then hidden when the "children" get called. Trouble is that I cannot find the event/action to trigger it to hide again when they get back to the root view....

我在根页面上有一个测试"按钮,可以手动执行操作,但它并不漂亮,我希望它是自动的.

I have a "test" button on the root page that manually does the action but it is not pretty and I want it to be automatic.

-(void)hideBar 
{
    self.navController.navigationBarHidden = YES;
}
-(void)showBar 
{       
    self.navController.navigationBarHidden = NO;
}

推荐答案

我发现的最好的解决方案是在第一个视图控制器中执行以下操作.

The nicest solution I have found is to do the following in the first view controller.

目标 C

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

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [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)
} 

当您将下一个 UIViewController 推入堆栈时,这将导致导航栏从左侧开始动画(与下一个视图一起),并将动画移向左侧(与旧视图一起)查看),当您按下 UINavigationBar 上的后退按钮时.

This will cause the navigation bar to animate in from the left (together with the next view) when you push the next UIViewController on the stack, and animate away to the left (together with the old view), when you press the back button on the UINavigationBar.

另请注意,这些不是委托方法,您正在覆盖 UIViewController 对这些方法的实现,并且根据文档,您必须在实现中的某处调用 super 的实现.

Please note also that these are not delegate methods, you are overriding UIViewController's implementation of these methods, and according to the documentation you must call the super's implementation somewhere in your implementation.

这篇关于iPhone仅在第一页隐藏导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆