在开发中我们经常会用到在导航控制器中实现左侧滑动返回,系统默认是支持的,但是当我们自定义NavigationController左侧按钮时,滑动返回就会失效,这个时候应该怎么办呢?
首先我们自定义一个继承自NavigationController的类,直接上代码
#import "FXQNavigationController.h"
@interface FXQNavigationController ()
@property (strong,nonatomic) id popDelegate;
@end
@implementation FXQNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
self.popDelegate = self.interactivePopGestureRecognizer.delegate;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers[0] == viewController) {
self.interactivePopGestureRecognizer.delegate = self.popDelegate;
}else{
self.interactivePopGestureRecognizer.delegate = nil;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
[super pushViewController:viewController animated:animated];
if (self.viewControllers.count > 1) {
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}
}
- (void)back{
[self popViewControllerAnimated:YES];
}
自己成为自己的代理,在OC中是可以的,这样就可以监听控制器的跳转,通过判断控制器是否是根控制器来控制手势的代理是否为空。希望对大家有所帮助
网友评论