导航栏显示/隐藏
我有一个带有导航栏的应用程序,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这不是几行代码就能解决的问题,但这是一种可能适合您的方法。
要隐藏导航栏:
要显示它:
此方法的文档为 可以在这里找到。
要侦听“双击”或双击,请子类化
UIView
并为该子类创建视图控制器的view
属性的实例。在视图子类中,覆盖其
-touchesEnded:withEvent:
方法并通过测量两次连续点击之间的时间来计算在一段时间内获得的触摸次数,也许使用CACurrentMediaTime()
。或者测试[touch tapCount]
。如果您点击两次,您的子类视图会发出
NSNotification
你的视图控制器已经注册监听。当您的视图控制器听到通知时,它会触发一个选择器,该选择器使用上述代码隐藏或显示导航栏,具体取决于导航栏当前的可见状态,通过读取导航栏的
isHidden
属性。编辑
我对处理点击事件的回答部分可能在 iOS 3.1 之前有用。
UIGestureRecognizer
类可能是更好的方法如今,用于处理双击。编辑2
隐藏导航栏的快速方法是:
显示它:
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:
To show it:
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'sview
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 withCACurrentMediaTime()
. 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:
To show it:
这段代码会对你有所帮助。
This code will help you.
首先阅读 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.
在 Swift 中尝试这个,
或者
In Swift try this,
or
隐藏导航栏:
显示导航栏:
To hide Navigation bar :
To show Navigation bar :
这是一个非常快速且简单的解决方案:
这将在单击而不是双击上起作用。即使在推送或弹出当前视图控制器之后,它也会改变导航控制器的行为。
如果您只想为单个视图控制器设置行为,则始终可以在控制器中的 viewWillAppear: 和 viewWillDisappear: 操作中修改此行为。
这是 文档:
Here is a very quick and simple solution:
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:
一种方法是在属性检查器中取消选中栏可见性“显示导航栏”。希望这对某人有帮助。
One way could be by unchecking Bar Visibility "Shows Navigation Bar" In Attribute Inspector.Hope this help someone.
在 Swift 4.2 和 Xcode 10 中,
如果您不想仅在第一个 VC 中显示导航栏,但希望在第二个 VC onword 中显示,请
在第一个 VC 中编写此代码。
In Swift 4.2 and Xcode 10
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.
UINavigationController 属性上的
hidesBarsOnTap
可以通过 iOS8 SDK 来处理这个问题https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instp /UINavigationController/hidesBarsOnTap
hidesBarsOnTap
on UINavigationController property come out to handle just this with iOS8 SDKhttps://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instp/UINavigationController/hidesBarsOnTap
SWIFT 代码:这完全适用于 iOS 3.2 及更高版本。
然后写
SWIFT CODE: This works fully for iOS 3.2 and later.
then write