自定义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
属性访问您的自定义项目。
- 如何模仿UITableView的UITableViewStylePlain部分标题样式
- 在Objective-C中将CGPoint转换为GLKVector2
- UINavigationController方法setToolbarHidden在Xcode 9中的bug:自动布局约束的无限计算导致OOM
- UITextFieldinput文本时出现奇怪的行为
- NSDateFormatter在iOS 5.1和iOS 6中的行为
- 在UICollectionView中设置边框
- 将NSDecimalNumber转换为NSString
- 如何隐藏IQKeyboardManager中的工具栏iOS Swift 3
- Swift NSTimer将用户信息检索为CGPoint