iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的,那么你需要自定义UINavigationBar,但当你自定义navigationBar后,这个功能就会自动失效。

屏蔽右滑返回功能代码:

  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  2. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  3. }

开启滑动返回功能代码:

  1. - (void)viewWillAppear:(BOOL)animated{
  2. [super viewWillAppear:animated];
  3. // 右滑返回
  4. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  5. self.navigationController.interactivePopGestureRecognizer.delegate = nil;
  6. }
  7. }

注意各种坑:

"在一级视图中,iOS样式返回的手势滑动一下,然后进入二级视图,发现画面卡住了,按Home键转入后台,再返回应用,发现并没有Crash掉,而是直接跳到了二级视图里,运行正常了,大家知道push和pop的原理是用进栈出栈完成的,可能因为在一级视图中滑动那一下,影响了视图在栈中的位置。 "

------有人提到通过以下方法处理:“一级视图中一定要加入self.navigationController.interactivePopGestureRecognizer.enabled = NO;,先把iOS7手势返回屏蔽掉,到二级视图再用self.navigationController.interactivePopGestureRecognizer.enabled = YES打开”

自己写了个demo试运行,发现self.navigationController.interactivePopGestureRecognizer.enabled 不能动态设置更改状态。因此该方法不可行。

解决方法:

  1. - (void)viewDidAppear:(BOOL)animated
  2. {
  3. __weak typeof(self) weakSelf = self;
  4. self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
  5. }

实现手势协议:

  1. #pragma mark - UIGestureRecognizerDelegate
  2. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer{
  3. //判断是否为rootViewController
  4. if (self.navigationController && self.navigationController.viewControllers.count == 1) {
  5. return NO;
  6. }
  7. return YES;
  8. }

但问题又来了,如果是一个显示成功/失败结果页,滑动返回不大符合正常思维,因为需要选择性屏蔽处理。

终极解决方法:自定义全屏滑动手势UIPanGestureRecognizer

    1. //
    2. //  BasicNavigationController.m
    3. //
    4. //
    5. //  Copyright (c) 2016年 lvxiangan520@126.com. All rights reserved.
    6. //
    7. #import "BasicNavigationController.h"
    8. #import "BaseResultViewController.h"
    9. @interface BasicNavigationController() <UIGestureRecognizerDelegate>
    10. @end
    11. @implementation BasicNavigationController
    12. - (void)viewDidLoad
    13. {
    14. [super viewDidLoad];
    15. [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : WhiteColor}];
    16. // 获取系统自带滑动手势的target对象
    17. id target = self.interactivePopGestureRecognizer.delegate;
    18. // 创建全屏滑动手势,调用系统自带滑动手势的target的action方法
    19. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    20. // 设置手势代理,拦截手势触发
    21. pan.delegate = self;
    22. // 给导航控制器的view添加全屏滑动手势
    23. [self.view addGestureRecognizer:pan];
    24. // 禁止使用系统自带的滑动手势
    25. self.interactivePopGestureRecognizer.enabled = NO;
    26. }
    27. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    28. {
    29. [viewController.navigationItem.backBarButtonItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:Scale_Size_Smaller()]} forState:UIControlStateNormal];
    30. if (self.childViewControllers.count > 0) {
    31. viewController.hidesBottomBarWhenPushed = YES;
    32. }
    33. [super pushViewController:viewController animated:YES];
    34. }
    35. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    36. {
    37. // 注意:只有非根控制器才有滑动返回功能,根控制器没有。
    38. // 判断导航控制器是否只有一个子控制器,如果只有一个子控制器,肯定是根控制器
    39. if (self.childViewControllers.count == 1) {
    40. // 表示用户在根控制器界面,就不需要触发滑动手势,
    41. return NO;
    42. }
    43. // 当前页面是显示结果页,不响应滑动手势
    44. UIViewController *vc = [self.childViewControllers lastObject];
    45. if ([vc isKindOfClass:[BaseResultViewController class]]) {
    46. return NO;
    47. }
    48. return YES;
    49. }
    50. @end

===========================实现app侧滑返回的方案二=================================

在需要开启侧滑返回的viewdidload里面写上    self.navigationController.interactivePopGestureRecognizer.delegate = nil;

或者干脆在  整个app的基类控制器里写上这句。

在每个navi的rootviewcontroller里面写上如下,这两个方法里的东西,是解决在navi根控制器侧滑的时候 出现的视觉bug

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}

}

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

}

}

至于,在业务逻辑上不可逆的成功或者失败的结果(比如点击返回要跳转首页,而不是单纯的pop当前页)页侧滑会返回上一页的问题的解决方案如下:

在成功的结果页面,把导航的控制器数组里面的不需要的控制器移除即可。

比如从  a-PUSH-b-PUSH-c-PUSH-d(成功页面, 点击返回和侧滑返回都需要返回到a,不能返回b和c)

NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];

for (int i=0; i<marr.count-1; i++) {

UIViewController * VC = marr[i];

if ([VC isKindOfClass:[B class]]) {

[marr removeObject:VC];

}

}

for (int i=0; i<marr.count-1; i++) {

UIViewController * VC = marr[i];

if ([VC isKindOfClass:[c class]]) {

[marr removeObject:VC];

}

}

self.navigationController.viewControllers = marr;

大家是通过方案一 还是  方案二 解决的,可以留言告诉我。

=======================================================

扩展:系统自带的向右滑动手势返回上一个界面,ios7--手势

当从控制器A push到控制器B,我们返回控制器A,除了使用按钮返回

[self.navigationController pushViewController:Vc animated:YES];

还可以使用ios7出来的向右滑动,返回控制器A

文档中是这样定义的:

@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;

@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

----------------------------------------------------------------------

我们在控制器B中的viewDidLoad中

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES; // 手势有效设置为YES 无效为NO
self.navigationController.interactivePopGestureRecognizer.delegate = self; // 手势的代理设置为self
}

但是当回到控制器A中时,再想push到控制器B,就会出现卡屏,不会动的现象,因为rootView也会有向右滑动返回的问题

要解决这个问题,我们只需在控制器A的viewDidAppear中设置,interactivePopGestureRecognizer为NO:

-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
} }

这样即可以保证再B中向右滑返回A动后再次pushB时不会卡在A界面。

推荐大家个朋友开的淘宝小店店, 欢迎光临

https://shop545764523.taobao.com/

iOS之手势滑动返回功能-b的更多相关文章

  1. iOS之手势滑动返回功能

    iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...

  2. 再谈iOS 7的手势滑动返回功能

    本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147  之前随手写过一篇<使用UIScreenEdgePanGestureR ...

  3. 禁用ios7 手势滑动返回功能

    禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...

  4. ios 侧边手势滑动返回 禁用/开启 功能

    // 禁用  返回手势       if ([self.navigationController respondsToSelector:@selector(interactivePopGestureR ...

  5. iOS 7的手势滑动返回

    如今使用默认模板创建的iOS App都支持手势返回功能,假设导航栏的返回button是自己定义的那么则会失效,也能够參考这里手动设置无效. if ([self.navigationController ...

  6. iOS彩票项目--第五天,新特性引导页的封装、返回按钮的自定义、导航控制器的滑动返回以及自定义滑动返回功能

    一.上次实现了在AppDelegate中通过判断app版本决定是否进入新特性页面,今天将AppDelegate中的一坨进行了封装.将self.window的根控制器到底应该为新特性界面,还是主页面,封 ...

  7. UINavigationController实现全屏滑动返回功能

    说明: UINavigationController默认在push出的控制器中都有边沿滑动返回功能,但是只能从屏幕左边滑才能返回,若从屏幕中间画并没有效果.下面实现全屏滑动功能. 探究: 系统默认能够 ...

  8. Android-通过SlidingMenu高仿微信6.2最新版手势滑动返回(二)

    转载请标明出处: http://blog.csdn.net/hanhailong726188/article/details/46453627 本文出自:[海龙的博客] 一.概述 在上一篇博文中,博文 ...

  9. Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)

    近期更新了微信版本号到6.2.发现里面有个很好的体验,就是在第二个页面Activity能手势向右滑动返回,在手势滑动的过程中能看到第一个页面,这样的体验很赞,这里高仿了一下. 这里使用的是v4包里面的 ...

随机推荐

  1. ubuntu上如何安装和卸载google chrome 浏览器

    切换到安装文件目录 $ sudo dpkg -i file_name.deb 如果有错误,请运行以下命令 $ sudo apt-get -f install or $ sudo apt-get ins ...

  2. android102 查询,插入联系人

    package com.itheima.getcontacts; import com.itheima.getcontacts.domain.Contact; import android.net.U ...

  3. TCP/IP 中的二进制反码求和算法

    对于这个算法,很多书上只是说一下思路,没有具体的实现.我在这里举个例子吧 以4bit(计算方便一点,和16bit是一样的)做检验和来验证. 建设原始数据为 1100 , 1010 , 0000(校验位 ...

  4. linux上安装shell编辑器与linux运维面试题

    分两个部分 一.安装B-shell解释器 安装cygwin  Eclipse要找到安装的bin路径 https://cygwin.com 二.安装编辑器shellEd 下载可以得到一个:net.sou ...

  5. 分享功能使用的UIPopoverController在iOS9 过期,替换为popoverPresentationController

    记录一下 以备以后用到的时候拿出来看看.以前使用的: 1 if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom ...

  6. iOS之AlertController的使用

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...

  7. mysql sqlmap 注入尝试

    假设注入点为 http://www.abc.com/news.php?id=12 //探测数据库信息 sqlmap -u http://www.abc.com/news.php?id=12 –dbs ...

  8. (转)Asp.NetURL重写的一种方法

    说到不用设置iis,主要是为了实现在虚拟主机或是拿不到iis操作限的时候,不能添加isap又想实现类似于静态化的程序实现方式,先声明,这里最终要实现的效果是,最终可以用 12345.html 替换 s ...

  9. JS操作CSS样式

    一.样式表(css) 使用样式表可以更好的显示WEB文档,也可以结合javascript从而实现很好的控制样式表. 样式(css)与内容(html): HTML是处理文档结构的,HTML可以实现如何把 ...

  10. XMLHttpRequest cannot load的问题解决方法

      在chrome中可以用--allow-file-access-from-files 命令来解决这个问题.右键点击chrome的快捷方式选择属性.在目标一栏中添加--allow-file-acces ...