美文网首页
自定义导航栏返回键 右滑返回失效

自定义导航栏返回键 右滑返回失效

作者: edison0428 | 来源:发表于2018-07-25 17:06 被阅读10次

自定义返回按钮时 右滑返回将会失效

解决方法1

我是直接在集成的UINavigaionController里直接加一句代码

- (void)viewDidLoad {
    [super viewDidLoad];
//遵守代理
    self.interactivePopGestureRecognizer.delegate = self;
}
解决方法1

也可以自己响应手势的事件

[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
}

如上两种方法不推荐使用,因为,比如说在rootViewController的时候这个手势也可以响应,导致整个程序页面不响应;如果在push的同时我触发这个手势,那么会导致navigationBar错乱,甚至crash

解决方法3

也是在集成UINavigaionController

@interface EYNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@property(nonatomic,weak) UIViewController* currentShowVC;

@end

-(id)initWithRootViewController:(UIViewController *)rootViewController {
    EYNavigationController *nvc= [super initWithRootViewController:rootViewController];
    self.interactivePopGestureRecognizer.delegate = self;
    nvc.delegate = self;
    return nvc;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (navigationController.viewControllers.count == 1)
        self.currentShowVC = nil;
    else {
        self.currentShowVC = viewController;
    }
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        return (self.currentShowVC == self.topViewController);
    }
    return YES;
}
全屏返回请看全屏返回

相关文章

网友评论

      本文标题:自定义导航栏返回键 右滑返回失效

      本文链接:https://www.haomeiwen.com/subject/vntmmftx.html