interactivePopGestureRecognizer ,右滑返回,自定义返回,多个tab

本文介绍了如何在iOS应用中处理右滑返回功能,特别是在存在多个TabBarController的情况下。当遇到rootViewController、pop动画进行或左滑时,阻止默认的手势执行。通过禁用navigationController的interactivePopGestureRecognizer并使用自定义的UIPanGestureRecognizer作为其代理来实现自定义返回行为。此外,针对自定义导航栏返回按钮和仅包含多个TabBarController的场景,提供了特定的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

苹果人机交互,在iOS7及其以上系统中,新增加了一个人机交互小的功能,也就是这个api:
self.navigationController.interactivePopGestureRecognizer.enabled

这个api功能就是在NavigationController堆栈内的UIViewController可以支持右滑手势,就是不用点击右上角的返回按钮,在屏幕最偏左边往右滑动,屏幕就会返回上一层级,这个功能的强大,让咱么这些拥有小巧玲珑并且又美丽的双手的用户有了更好的体验

总的来说有三种情况下手势不应该执行:

  • 当前controller是rootViewController时
  • pop动画正在进行时
  • 左滑时

另外。
该文采取的办法是对navigationController自带的interactivePopGestureRecognizer下手,禁用interactivePopGestureRecognizer,给interactivePopGestureRecognizer的delegate换上自定义的UIPanGestureRecognizer。




现在对这个手势问题分两中情况进行分析 :

(1、自定义导航栏返回按钮leftBarButtonItem  2、项目中嵌套多个UITabBarController),
   ps: 非以上两种情况的,不需要进行其他设置,系统自带右滑返回可以正常使用

***********************************************************************
情况一:    (自定义导航栏返回按钮leftBarButtonItem  且   项目中只有一个UITabBarController)

解决办法 :只需要在自定义返回按钮的ViewController中,在 viewDidLoad 方法中设置以下代码即

self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

***********************************************************************

情况二: (  自定义导航栏返回按钮leftBarButtonItem  且   项目中只有多个UITabBarController

     或者  自定义导航栏返回按钮leftBarButtonItem  且   项目中只有多个UITabBarController )


解决办法: 

1、在初始化UITabBarController的类TabVCA ,然后调用 push 方法推出该 TabVCA 的代码之后加上代码,如:

[self.navigationController pushViewController:[TabViewControllerA new] animated:YES];

 if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    }
2、接着步骤1中推出的TabVCA中的viewControllers,比方说改TabVCA中包含(X,Y,Z)三个导航的rootController ,你需要在这三个主VC中分别设置 :
   <1> 在viewDidLoad方法中设置以下代码

self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
  <2> 在viewDidAppear方法中设置以下代码(前提改VC是该Tab中的某个Navi)

self.navigationController.navigationBar.hidden = NO;//该方法禁止使用右滑手势,这样可以避免最底层VC右滑时出现的App假死状态
<3> 在viewWillDisappear方法中设置以下代码

self.navigationController.interactivePopGestureRecognizer.enabled = YES;//开启右滑手势

***********************************************************************

附 :自定义返回按钮   

UIButton *leftBackItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
[leftBackItem setImage:[UIImage imageNamed:@"messageChatBackIcon@2x.png"] forState:UIControlStateNormal];
[leftBackItem addTarget:self action:@selector(rongYunBackItemAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBackItem];
    
self.navigationItem.leftBarButtonItem = backButtonItem;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值