前提条件,存在A和B两个页面,A是主界面,A push 到 B
方法一:如果B页面的返回按钮要用自定义的按钮(在iOS7中,如果使用了UINavigationController,那么系统自带的附加了一个从屏幕左边缘开始滑动可以实现pop的手势。但是,如果自定义了navigationItem的leftBarButtonItem,那么这个手势就会失效。)
在A界面:
- (void)viewDidAppear:(BOOL)animated {
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;//重新设置代理
self.navigationController.interactivePopGestureRecognizer.enabled = NO;//本页面不能右滑
}
在B里面:
设置返回按钮:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(5, 2, 21, 35);
[backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
[[backBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
[self.navigationController popViewControllerAnimated:YES];
}];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = backItem;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;//最重要的一步
方法二:在A页面里面(A页面里面的backBarButtonItem其实就是我们看到的B页面的返回按钮,B页面的leftBar是左侧的按钮,注意优先级,B里面有自定义leftBar,显示B页面的,如果没有,看A界面,如果里面有backBarButtonItem,显示,如果没有,就是系统默认的那个)
UIImage* image = [UIImage imageNamed:@"back"];
UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
[item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[item setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0) forBarMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = item;
self.navigationItem.backBarButtonItem = item;
我采用的是方法一,方法二我没有办法调整返回按钮的位置