1.简介
使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使用导航栏push出一个控制器,我们在控制器中自定义了一个返回按钮.这样系统默认的滑动返回手势效果就没有了.
2.解决方法
[1]从A这个控制器push到B这个控制器,我们想要自定义B的返回按钮,我们可以在A中设置
1 self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"王俨" style:UIBarButtonItemStylePlain target:nil action:nil];
[2]在B控制器设置返回按钮,我想这应该是大多数人喜欢的选择,可是当我们在B控制器中设置self.navigationItem.leftBarButtonItem
后发现系统的滑动返回手势失效了.那么如何可以保留系统的滑动返回手势了,我们可以B控制器中这样做.
1 @interface ViewController () <UIGestureRecognizerDelegate>
2 @end
3
4 @implementation ViewController {
5 id<UIGestureRecognizerDelegate> _delegate
6 }
7
8 - (void)viewDidLoad {
9 [super viewDidLoad];
10
11 // 自定义返回按钮
12 UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
13 [button setTitle:@"王俨2" forState:UIControlStateNormal];
14 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
15 [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
16
17 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
18 }
19
20 - (void)back:(UIButton *)button {
21 [self.navigationController popViewControllerAnimated:YES];
22 }
23
24 - (void)viewWillAppear:(BOOL)animated {
25 [super viewWillAppear:animated];
26
27 if (self.navigationController.viewControllers.count > 1) {
28 // 记录系统返回手势的代理
29 _delegate = self.navigationController.interactivePopGestureRecognizer.delegate;
30 // 设置系统返回手势的代理为当前控制器
31 self.navigationController.interactivePopGestureRecognizer.delegate = self;
32 }
33 }
34
35 - (void)viewWillDisappear:(BOOL)animated {
36 [super viewWillDisappear:animated];
37
38 // 设置系统返回手势的代理为我们刚进入控制器的时候记录的系统的返回手势代理
39 self.navigationController.interactivePopGestureRecognizer.delegate = _delegate;
40 }
41
42 #pragma mark - UIGestureRecognizerDelegate
43 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
44 return self.navigationController.childViewControllers.count > 1;
45 }
46
47 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
48 return self.navigationController.viewControllers.count > 1;
49 }
50 @end
文/倚楼听风雨wing(简书作者)
原文链接:http://www.jianshu.com/p/33ce1340a543
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文链接:http://www.jianshu.com/p/33ce1340a543
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。