xcode 点击屏幕时隐藏/取消隐藏 UINavigationbar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8202852/
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
Hide/Unhide UINavigationbar when the screen is tapped
提问by Edoardo
I'm very new with iOS Development and I have just created one of my first apps, in my .xib file I have a UINavigationBar
that I want to hide/show when a part of the screen is tapped by the user (like in the Photo app). I've found some snippets online but I don't know where and how to use those.
我是 iOS 开发的新手,我刚刚创建了我的第一个应用程序,在我的 .xib 文件中,我有一个UINavigationBar
我想在用户点击屏幕的一部分时隐藏/显示的应用程序(如照片应用程序)。我在网上找到了一些片段,但我不知道在哪里以及如何使用它们。
I'd appreciate a lot if somebody could give me detailed informations about how to do this.
如果有人能给我提供有关如何执行此操作的详细信息,我将不胜感激。
回答by Jano
Add this toggle method anywhere in your UIViewController. This hides on first tap and shows again in second tap.
在 UIViewController 的任何地方添加这个切换方法。这在第一次点击时隐藏,并在第二次点击时再次显示。
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navigationController.navigationBar.hidden;
[self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
}
If there is no navigation controller, link the navigation bar with an IBOutlet and replace with
如果没有导航控制器,将导航栏与 IBOutlet 链接并替换为
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navBar.hidden;
self.navBar.hidden = !barsHidden;
}
Then add the following in the method -(void)viewDidLoad {}
然后在方法中添加以下内容 -(void)viewDidLoad {}
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavBar:)];
[self.view addGestureRecognizer:gesture];
[gesture release];
If the view where you are going to tap is a UIWebViewController, you have to add the protocol to the view controller and set it as delegate gesture.delegate = self;
then add the following:
如果您要点击的视图是 UIWebViewController,则必须将协议添加到视图控制器并将其设置为委托,gesture.delegate = self;
然后添加以下内容:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This is needed because the UIWebViewController already implements its own gesture recognizers.
这是必需的,因为 UIWebViewController 已经实现了它自己的手势识别器。
回答by jemmons
Ultimately, you want to send the -setHidden:
message to your navigation bar. The easiest way to do this is to make an Outletand an Actionin your in your view controller. Then, in your .xib file, connect the navigation bar to the outlet and some button (even a large, full screen one) to the action.
最终,您希望将-setHidden:
消息发送到导航栏。最简单的方法是在视图控制器中创建一个Outlet和一个Action。然后,在您的 .xib 文件中,将导航栏连接到插座,并将一些按钮(甚至是大的全屏按钮)连接到操作。
Outlets and Actions are basic techniques used over and over in iOS (and Mac) programming, so if you don't understand them, best go read up on them now. Every beginning iOS/Mac programming book covers this topic as does Apple's own Getting Started guide(pay particular attention to the Configuring the Viewsection).
Outlets 和 Actions 是在 iOS(和 Mac)编程中反复使用的基本技术,所以如果你不理解它们,最好现在就去阅读它们。每本 iOS/Mac 编程入门书籍都涵盖了这个主题,Apple 自己的入门指南也是如此(特别注意配置视图部分)。
Inside your action, send a message to the outlet like so:
在您的操作中,向插座发送消息,如下所示:
-(void)myButtonAction:(id)sender{
[[self myNavigationBarOutlet] setHidden:YES];
}
This will hide the navigation bar whenever your button is tapped.
这将在您点击按钮时隐藏导航栏。
(This assumes you have a UINavigationBar
in your .xib like you say. These directions will be different if you're working with a UINavigationController
that manages its own UINavigationBar
)
(这假设您UINavigationBar
的 .xib 中有您所说的。如果您使用的UINavigationController
是管理自己的.xib,这些方向会有所不同UINavigationBar
)