iOS侧滑返回

iOS7之后系统提供了侧滑手势(interactivePopGestureRecognizer),即从屏幕左侧边缘滑起会pop回导航控制器栈的上个viewController。不过如果你自定义了UINavigationViewController或者自定义了返回按钮,系统自带的侧滑返回功能会失效。此时需要在自定义的UINavigationViewController中添加下面的代码解决:

self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

虽然这样实现了侧滑,但是在根控制器上面进行侧滑手势的时候会出现卡死的现象,解决方法:
1、自定义导航栏遵守UINavigationBarDelegate协议
2、实现UINavigationBarDelegate的两个方法

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    if (self.childViewControllers.count > 1) {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
    return YES;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
    if (self.childViewControllers.count == 1) {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}

你可能感兴趣的:(iOS侧滑返回)