iOS 导航栏自带页面右滑pop使用示例
iOS7之后苹果导航控制器增加了一个左侧边缘右滑pop的手势,在非栈底的控制器里面,从左边右滑之后相当于出栈(pop)样子。
以下设置均在自定义的UINavigationController里操作
//默认是YES设置为NO左侧右滑手势就不可用了 self.interactivePopGestureRecognizer.enabled=YES;
也可以通过手势实现右滑
遵守UIGestureRecognizerDelegate协议,设置代理对象,实现一下代理方法即可:
-(void)viewDidAppear:(BOOL)animated{ [superviewDidAppear:animated]; self.navigationController.interactivePopGestureRecognizer.delegate=self; } #pragmamark-UIGestureRecognizerDelegate -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer { if(self.viewControllers.count<=1){ returnNO; } returnYES; }
个人的记忆方法:
backBarButtonItem设置下一个页面的返回按钮
leftBarButtonItem设置当前页面的返回按钮
项目中我们多数会自定义左侧返回按钮leftBarButtonItem,这个时候就算设置self.interactivePopGestureRecognizer.enabled=YES了或者实现手势的代理,左边缘的右滑动手势依旧会失效,例如如下代码写过之后,可以验证一下,失效了。
-(void)pushViewController:(UIViewController*)viewControlleranimated:(BOOL)animated { if(self.viewControllers.count>0){ /*不是栈底控制器的时候隐藏tabbar*/ viewController.hidesBottomBarWhenPushed=YES; //设置左边的返回按钮leftBarButtonItem UIButton*backBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; backBtn.frame=CGRectMake(0,0,44,44); backBtn.imageView.contentMode=UIViewContentModeCenter; backBtn.contentEdgeInsets=UIEdgeInsetsMake(0,0,0,34); [backBtnsetImage:[UIImageimageNamed:@"rrrr"]forState:UIControlStateNormal]; [backBtnsetImage:[UIImageimageNamed:@"rrrr"]forState:UIControlStateHighlighted]; [backBtnaddTarget:selfaction:@selector(back)forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem*backBtnItem=[[UIBarButtonItemalloc]initWithCustomView:backBtn]; viewController.navigationItem.leftBarButtonItem=backBtnItem; //此处改为设置backBarButtonItem右滑手势可以用,但自定义按钮不展示!! } [superpushViewController:viewControlleranimated:animated]; }
按照如下方式设置backBarButtonItem或leftBarButtonItem,其他不做任何设置,右滑效果可用:
-(void)pushViewController:(UIViewController*)viewControlleranimated:(BOOL)animated { if(self.viewControllers.count>0){ /*不是栈底控制器的时候隐藏tabbar*/ viewController.hidesBottomBarWhenPushed=YES; } [self.navigationBarsetBackIndicatorImage:[UIImageimageNamed:@"rrrr"]]; [self.navigationBarsetBackIndicatorTransitionMaskImage:[UIImageimageNamed:@"rrrr"]]; UIBarButtonItem*backItem=[[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:nilaction:nil]; self.navigationItem.backBarButtonItem=backItem; [superpushViewController:viewControlleranimated:animated]; }
或者
-(void)pushViewController:(UIViewController*)viewControlleranimated:(BOOL)animated { if(self.viewControllers.count>0){ /*不是栈底控制器的时候隐藏tabbar*/ viewController.hidesBottomBarWhenPushed=YES; [self.navigationBarsetBackIndicatorImage:[UIImageimageNamed:@"rrrr"]]; [self.navigationBarsetBackIndicatorTransitionMaskImage:[UIImageimageNamed:@"rrrr"]]; UIBarButtonItem*backItem=[[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:nilaction:nil]; self.navigationItem.leftBarButtonItem=backItem; } [superpushViewController:viewControlleranimated:animated]; }
进过上面的反复测试:在自定义导航栏控制器中backBarButtonItem不能用View视图方法自定义,而leftBarButtonItem可以。
setBackIndicatorImage和setBackIndicatorTransitionMaskImage无论怎么设置都是只能看到setBackIndicatorImage的图片,找不到原因!!!
然后尝试直接在root视图控制器里面设置返回按钮backBarButtonItem的自定义view,右滑手势可用,但是设置leftBarButtonItem就不能使用右滑手势:
-(void)viewDidLoad{ [superviewDidLoad]; //设置左边的返回按钮 UIButton*backBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; backBtn.frame=CGRectMake(0,0,44,44); backBtn.imageView.contentMode=UIViewContentModeCenter; backBtn.contentEdgeInsets=UIEdgeInsetsMake(0,0,0,34); [backBtnsetImage:[UIImageimageNamed:@"rrrr"]forState:UIControlStateNormal]; [backBtnsetImage:[UIImageimageNamed:@"rrrr"]forState:UIControlStateHighlighted]; [backBtnaddTarget:selfaction:@selector(back)forControlEvents:UIControlEventTouchUpInside]; backBtn.backgroundColor=[UIColorredColor]; [self.navigationController.navigationBarsetBackIndicatorImage:[UIImageimageNamed:@"rrrr"]]; [self.navigationController.navigationBarsetBackIndicatorTransitionMaskImage:[UIImageimageNamed:@"rrrr"]]; UIBarButtonItem*backBtnItem=[[UIBarButtonItemalloc]initWithCustomView:backBtn]; backBtnItem.title=@""; self.navigationItem.backBarButtonItem=backBtnItem; }
以上是自己的验证,希望对大家的学习有所帮助,也希望大家多多支持毛票票。