ios 导航栏显示/隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2926914/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:17:14  来源:igfitidea点击:

Navigation bar show/hide

iphoneiosuinavigationcontrolleruinavigationbar

提问by Shishir.bobby

I have an app with a navigation bar consisting of 2 bar buttons. I would like to hide and show this navigation bar when a user double taps the screen.

我有一个导航栏的应用程序,其中包含 2 个栏按钮。当用户双击屏幕时,我想隐藏和显示此导航栏。

Initially, the navigation bar should be hidden. When a user double taps the screen, the navigation bar should come up with an animation, like what can be seen in the iPhone's photo gallery.

最初,导航栏应该是隐藏的。当用户双击点击屏幕,导航栏应该拿出一个动画,像什么可以在iPhone的照片库中可以看出。

How can i do something this? Suggestions are always appreciated.

我怎么能做这样的事情?建议总是受到赞赏。

回答by Alex Reynolds

This isn't something that can fit into a few lines of code, but this is one approach that might work for you.

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

To hide the navigation bar:

隐藏导航栏:

[[self navigationController] setNavigationBarHidden:YES animated:YES];

To show it:

要显示它:

[[self navigationController] setNavigationBarHidden:NO animated:YES];

Documentation for this method is available here.

此方法的文档可在此处获得

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

要收听“双击”或双击,子类UIView,使该子类的视图控制器的实例view属性。

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].

在视图子类中,-touchesEnded:withEvent:通过测量两次连续点击之间的时间(可能使用CACurrentMediaTime(). 或从 测试结果[touch tapCount]

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

如果您点击两次,您的子类视图会发出NSNotification您的视图控制器已注册以侦听的 。

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 isHiddenproperty.

当您的视图控制器听到通知时,它会触发一个选择器,使用上述代码隐藏或显示导航栏,具体取决于导航栏的当前可见状态,通过阅读导航栏的isHidden属性访问。

EDIT

编辑

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

在 iOS 3.1 之前,我处理点击事件的部分答案可能很有用。该UIGestureRecognizer班可能是用于处理双水龙头,这几天的更好方法。

EDIT 2

编辑 2

The Swift way to hide the navigation bar is:

隐藏导航栏的 Swift 方法是:

self.navigationController?.setNavigationBarHidden(true, animated: true)

To show it:

要显示它:

self.navigationController?.setNavigationBarHidden(false, animated: true)

回答by Perjan Duro

This code will help you.

此代码将帮助您。

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];
}
}

回答by jclee

First read the the section in the View Controller Programming Guide for iOS about 'Adopting a Full-Screen Layout for Navigation Views' and the section about the same for Custom Views. If you are trying to do something like the Photos.app then you are probably using a scroll view. Note the comment that Navigation bars automatically add a scroll content inset to your scroll view to account for the height of the navigation bar (and status bar) so you have to reset the contentInset property of your scroll view back to zero (UIEdgeInsetsZero) right after setting up the initial state of the navigationBar and before the view appears.

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

Then if you have a single tap that toggles the navigationBar and/or status bar to show or hide, you need to do two things in you toggling method. The first seems to be to save the scroll view's contentOffset property before changing the NavigationBar hidden property and restore your saved value to contentOffset right afterward. And second to again zero out the contentInset property to UIEdgeInsetsZero after changing the navigationBarHidden property. Also, if you are toggling the status bar, you need to change its state before you change the navigationBar's state.

然后,如果您只需轻按一下即可切换导航栏和/或状态栏以显示或隐藏,您需要在切换方法中做两件事。第一个似乎是在更改 NavigationBar 隐藏属性之前保存滚动视图的 contentOffset 属性,然后立即将保存的值恢复为 contentOffset 。其次,在更改 navigationBarHidden 属性后,再次将 contentInset 属性归零为 UIEdgeInsetsZero。此外,如果您要切换状态栏,则需要在更改导航栏的状态之前更改其状态。

回答by Zaid Pathan

In Swifttry this,

Swift 中试试这个,

self.navigationController?.navigationBarHidden = true  //Hide
self.navigationController?.navigationBarHidden = false //Show

or

或者

self.navigationController?.setNavigationBarHidden(true, animated: true) //Hide
self.navigationController?.setNavigationBarHidden(false, animated: true) //SHow

回答by amayer171292591

Here is a very quick and simple solution:

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

self.navigationController.hidesBarsOnTap = YES;

This will work on single tap instead of double tap. Also it will change the behavior for the navigation controller even after pushing or popping the current view controller.

这将适用于单击而不是双击。即使在推送或弹出当前视图控制器之后,它也会改变导航控制器的行为。

You can always modify this behavior in your controller within the viewWillAppear: and viewWillDisappear: actions if you would like to set the behavior only for a single view controller.

如果您只想为单个视图控制器设置行为,您始终可以在 viewWillAppear: 和 viewWillDisappear: 操作中的控制器中修改此行为。

Here is the documentation:

这是文档

回答by Jayprakash Dubey

To hide Navigation bar :

隐藏导航栏:

[self.navigationController setNavigationBarHidden:YES animated:YES];

To show Navigation bar :

显示导航栏:

[self.navigationController setNavigationBarHidden:NO animated:YES];

回答by Avijit Nagare

One way could be by unchecking Bar Visibility "Shows Navigation Bar" In Attribute Inspector.Hope this help someone.

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

enter image description here

在此处输入图片说明

回答by iOS

In Swift 4.2 and Xcode 10

在 Swift 4.2 和 Xcode 10 中

self.navigationController?.isNavigationBarHidden = true  //Hide
self.navigationController?.isNavigationBarHidden = false  //Show

If you don't want to display Navigation bar only in 1st VC, but you want display in 2nd VC onword's

如果您不想仅在第一个 VC 中显示导航栏,但希望在第二个 VC onword 中显示

In your 1st VC write this code.

在您的第一个 VC 中编写此代码。

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true  //Hide
}

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = false  //Show
}

回答by Mohammad Kamran Usmani

If you want to detect the status of navigation bar wether it is hidden/shown. You can simply use following code to detect -

如果您想检测导航栏是否隐藏/显示的状态。您可以简单地使用以下代码来检测 -

if self.navigationController?.isNavigationBarHidden{
    print("Show navigation bar")
} else {
    print("hide navigation bar")
}