导航栏显示/隐藏

isc*_*ers 155 iphone uinavigationbar uinavigationcontroller ios

我有一个带有2个按钮的导航栏的应用程序.当用户双击屏幕时,我想隐藏并显示此导航栏.

最初,应隐藏导航栏.当用户双击屏幕时,导航栏应该会出现一个动画,就像iPhone的照片库中可以看到的那样.

我该怎么做呢?建议总是受到赞赏.

Ale*_*lds 379

这不是适合几行代码的东西,但这是一种可能适合您的方法.

要隐藏导航栏:

[[self navigationController] setNavigationBarHidden:YES animated:YES];
Run Code Online (Sandbox Code Playgroud)

要表明:

[[self navigationController] setNavigationBarHidden:NO animated:YES];
Run Code Online (Sandbox Code Playgroud)

此处提供了此方法的文档.

To listen for a "double click" or double-tap, subclass UIView and make an instance of that subclass your view controller's view property.

In the view subclass, override its -touchesEnded:withEvent: method and count how many touches you get in a duration of time, by measuring the time between two consecutive taps, perhaps with CACurrentMediaTime(). Or test the result from [touch tapCount].

If you get two taps, your subclassed view issues an NSNotification that your view controller has registered to listen for.

When your view controller hears the notification, it fires a selector that either hides or shows the navigation bar using the aforementioned code, depending on the navigation bar's current visible state, accessed through reading the navigation bar's isHidden property.

EDIT

The part of my answer for handling tap events is probably useful back before iOS 3.1. The UIGestureRecognizer class is probably a better approach for handling double-taps, these days.

EDIT 2

The Swift way to hide the navigation bar is:

self.navigationController?.setNavigationBarHidden(true, animated: true)
Run Code Online (Sandbox Code Playgroud)

To show it:

self.navigationController?.setNavigationBarHidden(false, animated: true)
Run Code Online (Sandbox Code Playgroud)


Per*_*uro 17

这段代码可以帮到你.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] 
initWithTarget:self action:@selector(showHideNavbar:)];
[self.view addGestureRecognizer:tapGesture];

-(void) showHideNavbar:(id) sender 
{ 
// write code to show/hide nav bar here 
// check if the Navigation Bar is shown
if (self.navigationController.navigationBar.hidden == NO)
{
// hide the Navigation Bar
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
// if Navigation Bar is already hidden
else if (self.navigationController.navigationBar.hidden == YES)
{
// Show the Navigation Bar
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
Run Code Online (Sandbox Code Playgroud)


jcl*_*lee 16

首先阅读"适用于iOS的View Controller编程指南"中有关"采用导航视图的全屏布局"的部分以及关于自定义视图的相关部分.如果您尝试执行类似Photos.app的操作,那么您可能正在使用滚动视图.请注意导航栏自动将滚动内容插入添加到滚动视图的注释,以考虑导航栏(和状态栏)的高度,因此您必须立即将滚动视图的contentInset属性重置为零(UIEdgeInsetsZero)设置navigationBar的初始状态并在视图出现之前.

然后,如果您只有一个点按切换导航栏和/或状态栏以显示或隐藏,您需要在切换方法中执行两项操作.第一个似乎是在更改NavigationBar隐藏属性之前保存滚动视图的contentOffset属性,然后将保存的值恢复到contentOffset.第二,在更改navigationBarHidden属性后,再次将contentInset属性清零为UIEdgeInsetsZero.此外,如果要切换状态栏,则需要在更改navigationBar状态之前更改其状态.


Zai*_*han 9

Swift试试这个,

self.navigationController?.navigationBarHidden = true  //Hide
self.navigationController?.navigationBarHidden = false //Show
Run Code Online (Sandbox Code Playgroud)

要么

self.navigationController?.setNavigationBarHidden(true, animated: true) //Hide
self.navigationController?.setNavigationBarHidden(false, animated: true) //SHow
Run Code Online (Sandbox Code Playgroud)


Jay*_*bey 7

要隐藏导航栏:

[self.navigationController setNavigationBarHidden:YES animated:YES];
Run Code Online (Sandbox Code Playgroud)

要显示导航栏:

[self.navigationController setNavigationBarHidden:NO animated:YES];
Run Code Online (Sandbox Code Playgroud)


ama*_*591 7

这是一个非常快速和简单的解决方案:

self.navigationController.hidesBarsOnTap = YES;
Run Code Online (Sandbox Code Playgroud)

这将适用于单击而非双击.此外,即使在按下或弹出当前视图控制器后,它也会更改导航控制器的行为.

如果您只想为单个视图控制器设置行为,则可以在viewWillAppear:和viewWillDisappear:actions中的控制器中修改此行为.

这是文档:


Avi*_*are 5

一种方法是在属性检查器中取消选中栏可见性“显示导航栏”。希望这对某人有所帮助。

在此处输入图片说明