自定义UIBackButtonItem和UINavigationController手势 [英] Custom UIBackButtonItem and UINavigationController gestures

查看:113
本文介绍了自定义UIBackButtonItem和UINavigationController手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带有自定义 UIBarButtonItem UINavigationBar

I need to have a UINavigationBar with custom UIBarButtonItem.

我知道怎么做(使用自定义视图),但有一个问题:

I know how to do it (with custom view), but there is one problem:

使用默认后退按钮项目为我们提供了 iOS 7 手势,因此我们可以滑动返回等,使用自定义 UIBarButtonItem 项目不会给我们这些姿势。

Using the default back button item gives us the iOS 7 gestures, so we can swipe to go back etc., using custom UIBarButtonItem item doesn't give us these gestures.

我们如何创建自定义 UIBarButtonItem 并维护 iOS 7 手势?

How can we create custom UIBarButtonItem and maintain the iOS 7 gestures ?

我不想从头开始构建整个滑动手势,我不相信这是唯一的方法。

推荐答案

手势基本上可以使用addGesture :( UIGestureRecognizer *)手势方法添加到任何UIView。

Gestures can be basically be added to any UIView using the addGesture:(UIGestureRecognizer *)gesture method.

基本上,您需要实例化UISwipeGestureRecognizer对象,设置您想要的任何属性并实现其委托。然后,只需将其添加到您希望识别UISwipeGestureRecognizer的视图中。

Basically, you need to instantiate a UISwipeGestureRecognizer object, set whatever properties you want and implement its delegate. Then, simply add it to the view on which you wish the UISwipeGestureRecognizer to be recognized.

因此,例如,由于UINavigationBar继承自UIView,您可以像这样发送addGesture:(UIGestureRecognizer *)手势消息:

So for instance, since UINavigationBar inherits from UIView, you can send the addGesture:(UIGestureRecognizer *)gesture message to it like so:

UINavigationBar *myNavigationBar = [UINavigationBar new];
[self.view addView:myNavigationBar]; // 'self' refers to your view controller assuming this is where your code lives
UISwipeGestureRecognizer *swipeGesture = [UISwipeGestureRecognizer new]; // use the designated initializer method instead
[myNavigationBar addGesture:swipeGesture]; // again, this method is inherited from UIView, it's how you add gestures to views
[myNavigationBar setUserInteractionEnabled:YES]; // this is very important for enabling gestures

你去了。

请记住,这是一半的工作,因为你想要实现动画,使它看起来像你正在刷页面,它会随着你的滑动移动。

Keep in mind this is kind of half the work because you want to implement animation to make it look like you're swiping a page and it moves as you are swiping.

这篇关于自定义UIBackButtonItem和UINavigationController手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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