首先 第 一 种情况:用系统默认的navigaController,再在viewController里自定义了左侧按钮,例如:
UIBarButtonItem *button = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonAction)];
self.navigationItem.leftBarButtonItem = button;
这样写之后会发现返回的手势就没有了.
加了如下方法就恢复了:
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
第 二 种情况:自定义的navigationController,再在viewController里自定义了左侧按钮,比如
自定义的navigationController里重写了下面的方法
+(void)initialize
{
UINavigationBar *navigationBar = [UINavigationBarappearance];
[navigationBar setBarTintColor:[UIColorpurpleColor]];
[navigationBar setTintColor:[UIColorwhiteColor]];
NSShadow *shadow = [[NSShadowalloc]init];
[shadow setShadowOffset:CGSizeZero];
[navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorwhiteColor],NSShadowAttributeName:shadow}];
//返回字体的颜色
UIBarButtonItem *barButtonItem = [UIBarButtonItemappearance];
[barButtonItem setTintColor:[UIColorgreenColor]];
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
}
在自定义的navigationController的 viewDidLoad里加入代码:
- (void)viewDidLoad {
[superviewDidLoad];
__weak typeof (self) weakSelf = self;
if ([selfrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//重新签代理
self.interactivePopGestureRecognizer.delegate = weakSelf;
}
}
这样在每个页面自定义左侧按钮时不需要任何操作就可以自带返回手势了(当然创建viewController时你得保证用的是自定义的navigationController作为导航控制器)