我们在开发过程中都会或多或少的遇到过自定义"返回"按钮后系统返回手势无法响应的情况,下面做一总结:
在iOS7系统中,自带了可以通过右滑返回上一级页面的手势,如果仅仅修改leftBarButtonItem是无法响应这个手势的。可以在pushViewController之后加入如下代码:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//在自定义leftBarButtonItem后添加下面代码就可以完美解决返回手势无效的情况
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
可以通过下面方式进行返回的事件的捕捉:
- (void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear: animated];
if (![[self.navigationController viewControllers] containsObject: self])
{
// the view has been removed from the navigation stack, back is probably the cause
// this will be slow with a large stack however.
}
}
至此,我们就完美解决了iOS7上使用leftBarButtonItem的滑动返回问题。
同时可以通过下面方式,来解决多次滑动之后会导致界面假死的情况:
在所有除一级页面之外的页面的viewDidAppear和viewWillDisappear中加入以下代码:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//代理置空,否则会闪退
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//开启iOS7的滑动返回效果
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//只有在二级页面生效
if ([self.navigationController.viewControllers count] == 2) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
}
同时在UINavigationcontroller的delegate中实现以下方法:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//开启滑动手势
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
在pushviewcontroller之前加入以下代码:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = NO;
}
[self.navigationController pushViewController:viewController animated:YES];
即可在实现滑动返回的同时,避免界面卡死的问题。
同时我们在应用中还会遇到一种情况:
在一个界面中会使用UIScrollView. 在这时,会阻挡系统的向右返回手势,可以通过下面方式解决:
- (void)viewDidLoad {
[super viewDidLoad];
.....
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
实现代理方法:
- (BOOL)gestureRecognizer: (UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer: (UIGestureRecognizer *)otherGestureRecognizer
{
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return NO;
}
当然不要忘记制空代理:<否则会崩溃>
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//代理置空,否则会闪退
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
参考:
1: http://blog.csdn.net/zhaoxy_thu/article/details/15811189
2: http://ju.outofmemory.cn/entry/103566
转载请标注出处. 尊重原创.