自定义UIBackButtonItem和UINavigationController手势

我需要与自定义的UIBarButtonItem UINavigationBar

我知道如何去做(自定义视图),但是有一个问题:

使用默认的后退button项给我们的iOS 7手势,所以我们可以刷卡回去等,使用自定义的UIBarButtonItem项目不给我们这些手势。

我们如何创build自定义的UIBarButtonItem并保持iOS 7手势?

我不想从头开始构build整个轻扫手势,我不相信这是唯一的方法。

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

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

因此,例如,由于UINavigationBar从UIViewinheritance,所以可以像这样发送addGesture:(UIGestureRecognizer *)手势消息:

 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 

你走了

请记住,这是一半的工作,因为你想要实现animation,让它看起来像你刷一个页面,并随着你的滑动而移动。

你可以使用一些小技巧来获得原生姿态。 创buildUINavigationItem的子类,然后覆盖leftBarButtonItems方法:

 - (NSArray*)leftBarButtonItems { return nil; } 

现在使用这个类的具有自定义左UIBarButtonItem的项目。 手势起作用! 这是因为UINavigationController认为没有剩余的项目,并启用手势。 您仍然可以通过leftBarButtonItem属性访问您的自定义项目。