@chenbinghua
2015-10-15T11:20:03.000000Z
字数 1840
阅读 2514
iOS笔记
当滑动返回失效时,有两种方法可以修复
自定义UINavigationController.m文件
遵循UINavigationControllerDelegate协议
- (void)viewDidLoad {
[super viewDidLoad];
// 还原滑动返回功能
self.popDelegate = self.interactivePopGestureRecognizer.delegate;
self.delegate = self;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count != 0) { // 非根控制器
viewController.hidesBottomBarWhenPushed = YES;
// 设置导航条左边按钮的内容
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
// 就有滑动返回功能
self.interactivePopGestureRecognizer.delegate = nil;
}
[super pushViewController:viewController animated:animated];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
// 如果展示的是根控制器是根控制器,就还原pop手势代理
if (viewController == [self.viewControllers firstObject]) {
self.interactivePopGestureRecognizer.delegate = self.popDelegate;
}
}
自定义UINavigationController.m文件
遵循UIGestureRecognizerDelegate协议
- (void)viewDidLoad {
[super viewDidLoad];
// 滑动返回相关
[self slideToBack];
}
- (void)slideToBack{
// 禁止系统原来的滑动返回手势,防止手势冲突
self.interactivePopGestureRecognizer.enabled = NO;
// 自定义滑动手势添加到self.view 调用系统原来的滑动返回方法
// 即self.interactivePopGestureRecognizer.delegate的handleNavigationTransition:方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
}
#pragma mark - 手势代理方法
// 是否开始触发手势,如果是根控制器就不触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 判断下当前控制器是否是根控制器
return (self.topViewController != [self.viewControllers firstObject]);
}