iOS-自定义导航栏后侧滑返回功能失效

从iOS7开始,系统为UINavigationController提供了一个interactivePopGestureRecognizer用于右滑返回(pop),但是,如果自定了当前视图控制器leftBarButtonItem,该手势就失效了。

解决方法:自定义UINavigationController,实现其代理方法:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

如果push的是非根控制器,设置self.interactivePopGestureRecognizer.delegate = nil;
如果是根控制器,将原来的代理重新设置即可。
具体代码如下:

#import "LGNavigationController.h"

@interface LGNavigationController ()<UINavigationControllerDelegate>

@property (nonatomic,strong) id popDelegate;

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    //代理    
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
}

#pragma UINavigationControllerDelegate方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //实现滑动返回功能
    //清空滑动返回手势的代理就能实现
     self.interactivePopGestureRecognizer.delegate =  viewController == self.viewControllers[0]? self.popDelegate : nil;
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容