导航栏显示/隐藏

发布于 2024-09-03 03:31:48 字数 138 浏览 2 评论 0原文

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

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

我该怎么做?建议始终受到赞赏。

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.

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.

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

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

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

发布评论

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

评论(11

¢蛋碎的人ぎ生 2024-09-10 03:31:48

这不是几行代码就能解决的问题,但这是一种可能适合您的方法。

要隐藏导航栏:

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

要显示它:

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

此方法的文档为 可以在这里找到

要侦听“双击”或双击,请子类化 UIView 并为该子类创建视图控制器的 view 属性的实例。

在视图子类中,覆盖其 -touchesEnded:withEvent: 方法并通过测量两次连续点击之间的时间来计算在一段时间内获得的触摸次数,也许使用 CACurrentMediaTime()。或者测试 [touch tapCount]

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

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

编辑

我对处理点击事件的回答部分可能在 iOS 3.1 之前有用。 UIGestureRecognizer 类可能是更好的方法如今,用于处理双击。

编辑2

隐藏导航栏的快速方法是:

navigationController?.setNavigationBarHidden(true, animated: true)

显示它:

navigationController?.setNavigationBarHidden(false, animated: true)

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 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:

navigationController?.setNavigationBarHidden(true, animated: true)

To show it:

navigationController?.setNavigationBarHidden(false, animated: true)
时光礼记 2024-09-10 03:31:48

这段代码会对你有所帮助。

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

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];
}
}
同尘 2024-09-10 03:31:48

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

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

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.

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.

七堇年 2024-09-10 03:31:48

Swift 中尝试这个,

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

或者

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

In Swift try this,

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

or

navigationController?.setNavigationBarHidden(true, animated: true) //Hide
navigationController?.setNavigationBarHidden(false, animated: true) //Show
傲鸠 2024-09-10 03:31:48

隐藏导航栏:

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

显示导航栏:

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

To hide Navigation bar :

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

To show Navigation bar :

[self.navigationController setNavigationBarHidden:NO animated:YES];
半寸时光 2024-09-10 03:31:48

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

self.navigationController.hidesBarsOnTap = YES;

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

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

这是 文档

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.

Here is the documentation:

孤凫 2024-09-10 03:31:48

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

输入图像描述这里

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

enter image description here

情绪少女 2024-09-10 03:31:48

在 Swift 4.2 和 Xcode 10 中,

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

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

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

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

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

In Swift 4.2 and 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

In your 1st VC write this code.

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

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = false  //Show
}
桃酥萝莉 2024-09-10 03:31:48

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

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

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")
}
药祭#氼 2024-09-10 03:31:48

SWIFT 代码:这完全适用于 iOS 3.2 及更高版本。

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let tapGesture = UITapGestureRecognizer(target: self, action: "hideNavBarOntap")let tapGesture = UITapGestureRecognizer(target: self, action: "hideNavBarOntap")
    tapGesture.delegate = self
    self.view.addGestureRecognizer(tapGesture)

然后写

func hideNavBarOntap() {
    if(self.navigationController?.navigationBar.hidden == false) {
        self.navigationController?.setNavigationBarHidden(true, animated: true) // hide nav bar is not hidden
    } else if(self.navigationController?.navigationBar.hidden == true) {
        self.navigationController?.setNavigationBarHidden(false, animated: true) // show nav bar
    }
}

SWIFT CODE: This works fully for iOS 3.2 and later.

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let tapGesture = UITapGestureRecognizer(target: self, action: "hideNavBarOntap")let tapGesture = UITapGestureRecognizer(target: self, action: "hideNavBarOntap")
    tapGesture.delegate = self
    self.view.addGestureRecognizer(tapGesture)

then write

func hideNavBarOntap() {
    if(self.navigationController?.navigationBar.hidden == false) {
        self.navigationController?.setNavigationBarHidden(true, animated: true) // hide nav bar is not hidden
    } else if(self.navigationController?.navigationBar.hidden == true) {
        self.navigationController?.setNavigationBarHidden(false, animated: true) // show nav bar
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文