导航栏显示/隐藏 [英] Navigation bar show/hide

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

问题描述

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

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.

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

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.

推荐答案

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

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

隐藏导航栏:

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

要显示:

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

此方法的文档是此处可用

要收听双击或双击,子类 UIView 并创建该子类的实例视图控制器的 view property。

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

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

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 -timeIntervalSinceDate:. Or test the result from [touch tapCount].

如果你有两个水龙头,您的子视图发出 <您的视图控制器已注册要侦听的code> NSNotification

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

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

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

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

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.

编辑2

隐藏导航栏的Swift方式是:

The Swift way to hide the navigation bar is:

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

要显示:

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

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

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