iOS7之后苹果导航控制器增加了一个左侧边缘右滑pop的手势,在非栈底的控制器里面,从左边右滑之后相当于出栈(pop)样子。
以下设置均在自定义的UINavigationController里操作
1
2
|
// 默认是YES 设置为NO左侧右滑手势就不可用了
self.interactivePopGestureRecognizer.enabled = YES;
|
也可以通过手势实现右滑
遵守UIGestureRecognizerDelegate协议,设置代理对象,实现一下代理方法即可:
1
2
3
4
5
6
7
8
9
10
11
12
|
-( void )viewDidAppear:( BOOL )animated {
[super viewDidAppear:animated];
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
#pragma mark - UIGestureRecognizerDelegate
- ( BOOL )gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (self.viewControllers.count <= 1) {
return NO;
}
return YES;
}
|
个人的记忆方法:
backBarButtonItem设置下一个页面的返回按钮
leftBarButtonItem设置当前页面的返回按钮
项目中我们多数会自定义左侧返回按钮leftBarButtonItem,这个时候就算设置self.interactivePopGestureRecognizer.enabled = YES了或者实现手势的代理,左边缘的右滑动手势依旧会失效,例如如下代码写过之后,可以验证一下,失效了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
- ( void )pushViewController:(UIViewController *)viewController animated:( BOOL )animated
{
if (self.viewControllers.count > 0) {
/* 不是栈底控制器的时候隐藏tabbar */
viewController.hidesBottomBarWhenPushed = YES;
// 设置左边的返回按钮leftBarButtonItem
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 44, 44);
backBtn.imageView.contentMode = UIViewContentModeCenter;
backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 34);
[backBtn setImage:[UIImage imageNamed:@ "rrrr" ] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@ "rrrr" ] forState:UIControlStateHighlighted];
[backBtn addTarget: self action: @selector(back) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *backBtnItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
viewController.navigationItem.leftBarButtonItem = backBtnItem;
// 此处改为设置backBarButtonItem 右滑手势可以用,但自定义按钮不展示!!
}
[super pushViewController:viewController animated:animated];
}
|
按照如下方式设置backBarButtonItem或leftBarButtonItem,其他不做任何设置,右滑效果可用:
1
2
3
4
5
6
7
8
9
10
11
12
|
- ( void )pushViewController:(UIViewController *)viewController animated:( BOOL )animated
{
if (self.viewControllers.count > 0) {
/* 不是栈底控制器的时候隐藏tabbar */
viewController.hidesBottomBarWhenPushed = YES;
}
[self.navigationBar setBackIndicatorImage:[UIImage imageNamed:@ "rrrr" ]];
[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@ "rrrr" ]];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@ "" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
[super pushViewController:viewController animated:animated];
}
|
或者
1
2
3
4
5
6
7
8
9
10
11
12
|
- ( void )pushViewController:(UIViewController *)viewController animated:( BOOL )animated
{
if (self.viewControllers.count > 0) {
/* 不是栈底控制器的时候隐藏tabbar */
viewController.hidesBottomBarWhenPushed = YES;
[self.navigationBar setBackIndicatorImage:[UIImage imageNamed:@ "rrrr" ]];
[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@ "rrrr" ]];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@ "" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftBarButtonItem = backItem;
}
[super pushViewController:viewController animated:animated];
}
|
进过上面的反复测试:在自定义导航栏控制器中backBarButtonItem不能用View视图方法自定义,而leftBarButtonItem可以。
setBackIndicatorImage和setBackIndicatorTransitionMaskImage无论怎么设置都是只能看到setBackIndicatorImage的图片,找不到原因!!!
然后尝试直接在root视图控制器里面设置返回按钮backBarButtonItem的自定义view,右滑手势可用,但是设置leftBarButtonItem就不能使用右滑手势:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
- ( void )viewDidLoad {
[super viewDidLoad];
// 设置左边的返回按钮
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 44, 44);
backBtn.imageView.contentMode = UIViewContentModeCenter;
backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 34);
[backBtn setImage:[UIImage imageNamed:@ "rrrr" ] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@ "rrrr" ] forState:UIControlStateHighlighted];
[backBtn addTarget: self action: @selector(back) forControlEvents: UIControlEventTouchUpInside];
backBtn.backgroundColor = [UIColor redColor];
[self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@ "rrrr" ]];
[self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@ "rrrr" ]];
UIBarButtonItem *backBtnItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
backBtnItem.title = @ "" ;
self.navigationItem.backBarButtonItem = backBtnItem;
}
|
以上是自己的验证,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cocoachina.com/ios/20171208/21474.html