欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS 导航栏自带页面右滑pop使用示例

时间:2021-06-22 09:36:30|栏目:iOS代码|点击:

iOS7之后苹果导航控制器增加了一个左侧边缘右滑pop的手势,在非栈底的控制器里面,从左边右滑之后相当于出栈(pop)样子。

以下设置均在自定义的UINavigationController里操作

// 默认是YES 设置为NO左侧右滑手势就不可用了
self.interactivePopGestureRecognizer.enabled = YES;

也可以通过手势实现右滑

遵守UIGestureRecognizerDelegate协议,设置代理对象,实现一下代理方法即可:

-(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了或者实现手势的代理,左边缘的右滑动手势依旧会失效,例如如下代码写过之后,可以验证一下,失效了。

- (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,其他不做任何设置,右滑效果可用:

- (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];
}

或者

- (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就不能使用右滑手势:

- (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;
  }

上一篇:详解iOS多线程之2.NSThread的加锁@synchronized

栏    目:iOS代码

下一篇:解决iOS11图片下拉放大出现信号栏白条的bug问题

本文标题:iOS 导航栏自带页面右滑pop使用示例

本文地址:http://www.codeinn.net/misctech/146262.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有