#import "LyonNavigationController.h"
@interface LyonNavigationController ()<UIGestureRecognizerDelegate>
@end
@implementation LyonNavigationController
// BUG:(实现自定义返回按钮覆盖掉系统按钮 要求还原导航栏自带的滑动返回手势功能)
// 如果单单在viewDidLoad方法中设置
// self.interactivePopGestureRecognizer.delegate = nil;
// 虽然还原了滑动手势,但是存在根控制器的也有滑动手势的bug
// 解决方法:
// 在viewDidLoad方法中
// 1. 将系统自带滑动手势代理赋给变量
// self.temDelegate = self.interactivePopGestureRecognizer.delegate;
// 2.设置navigationcontroller的代理方法
// self.delegate = self;
// 3.在push代理方法中还原滑动手势
// self.interactivePopGestureRecognizer.delegate = nil;
// 4.设置navigationcontroller的代理方法,在didshow为根控制器的时候设置返回手势失效
// self.interactivePopGestureRecognizer.delegate = self.temDelegate;
- (void)viewDidLoad {
[super viewDidLoad];
// 防止手势冲突
self.interactivePopGestureRecognizer.enabled = NO;
// handleNavigationTransition: 为系统手势触发方法
// 1. NSLog(@"%@",self.interactivePopGestureRecogniz);滑动的手势
// 2. 输出代理,获取handleNavigationTransition:方法
// 可通过运行时(RunTime.h)获取手势方法属性列表
// 通过_target属性使用 kvc 获取target对象
// 1. 由于target 等于 self.interactivePopGestureRecognizer.delegate
// 2. 所以本文使用 self.interactivePopGestureRecognizer.delegate
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
// 被夹在这中间的代码针对于此警告都会无视并且不显示出来
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
#pragma clang diagnostic pop
pan.delegate = self;
[self.view addGestureRecognizer:pan];
}
#pragma mark - 手势代理方法
// 是否开始触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 判断下当前控制器是否是跟控制器
return (self.topViewController != [self.viewControllers firstObject]);
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count != 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
@end
网友评论