NavigationControllerr滑动返回
iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势。支持此手势的是UINavigationController中新增的属性
interactivePopGestureRecognizer,即右滑返回只支持以UINavigationController为容器的ViewController间切换,要想在自定义容器中使用,需要一些额外的工作。
基本地,控制ViewController是否启用右滑返回,只需要这样:
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
默认情况下enabled为YES。
在实际使用中,遇到了一些问题,整理如下:
1、自定义返回按钮后,右滑返回失效;
解决方案:比较直观的办法是在自定义返回按钮时,使用backBarButtonItem:
UIButton backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//some initialize code here...
UIBarButtonItem barItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barItem; //not working
self.navigationItem.backBarButtonItem = barItem; //serve well
P.S:关于backBarButtonItem和leftBarButtonItem的区别:
http://www.cocoachina.com/ask/questions/show/97110
但这样无法支持左上角多个按钮的情况。考虑到 interactivePopGestureRecognizer也有delegate属性, 替换默认的 self . navigationController .interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。在主ViewController中:
self.navigationController.interactivePopGestureRecognizer.delegate = self;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
2 {
3 if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
4 {
5 return NO;
6 }
7 else
8 {
9 return YES;
10 }
11 }
如此做的好处是可以在主ViewController中配置栈中所有ViewController右滑返回的开启,而不需要在各个ViewController中分别设置enabled。
值得注意的是:在替换了delegate之后,必须在gestureRecognizerShouldBegin:中设置某ViewController A开启右滑返回,同时在A中未设置interactivePopGestureRecognizer.enabled = NO,右滑返回才会开启,即二者中任一为NO,右滑返回都处于关闭状态。
2、主界面(UINavigationController栈中的第一个ViewController)默认也是开启右滑返回的。若在主界面上右滑,不会有动作执行。但此时想进入下一级ViewController(如点击tableView中某一行),切换动画却没有出现。切回桌面再进入应用,发现直接进入了下一级ViewController。
解决方案:这个问题是在最初试验右滑返回的使用方式时出现的。在使用自定义返回按钮的ViewController中
self.navigationController.interactivePopGestureRecognizer.delegate = self;
解决解决问题1的同时,造成了问题2。和1中相似,都是在替换了默认的delegate之后,interactivePopGestureRecognizer就能调用自定义的返回方法了。具体原因尚不清楚,待更新【Mark】。
3、在使用右滑返回拖动到一半时,有时会在导航栏上看到三个排成一行的小蓝点。
解决方案:原因不明,解决方案不明。
P.S:在一个帖子上看到一个办法:
self.navigationItem.title = @"";
可以隐藏小蓝点,但由于小蓝点非必现,在不明究竟的情况下很难说是否有效。
帖子链接: http://www.tuicool.com/articles/FB3IJ3
(1)在工程中查看, self . navigationController .interactivePopGestureRecognizer.delegate实际上是一个
_UINavigationInteractiveTransition实例,该类声明如下:
@class UIScreenEdgePanGestureRecognizer;
2
3 @interface _UINavigationInteractiveTransition : _UINavigationInteractiveTransitionBase {
4 UIScreenEdgePanGestureRecognizer *_edgePanRecognizer;
5 }
6
7 @property(readonly) UIScreenEdgePanGestureRecognizer * screenEdgePanGestureRecognizer;
8
9 - (void)_configureNavigationGesture;
10 - (BOOL)_gestureRecognizer:(id)arg1 shouldBeRequiredToFailByGestureRecognizer:(id)arg2;
11 - (void)dealloc;
12 - (BOOL)gestureRecognizer:(id)arg1 shouldReceiveTouch:(id)arg2;
13 - (BOOL)gestureRecognizer:(id)arg1 shouldRecognizeSimultaneouslyWithGestureRecognizer:(id)arg2;
14 - (BOOL)gestureRecognizerShouldBegin:(id)arg1;
15 - (id)gestureRecognizerView;
16 - (id)initWithViewController:(id)arg1 animator:(id)arg2;
17 - (id)screenEdgePanGestureRecognizer;
18 - (void)setNotInteractiveTransition;
19 - (void)startInteractiveTransition;
20
21 @end
可以看到,委托的内部,实际上是一个UIScreenEdgePanGestureRecognizer实例在起作用,它是iOS7中引入的一个新类,用于支持某些情况下ViewController间切换的初始化。apple官方文档中对其的描述很少,如下:
A UIScreenEdgePanGestureRecognizer looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions.
After creating a screen edge pan gesture recognizer, assign an appropriate value to the edges property before attaching the gesture recognizer to your view. You use this property to specify from which edges the gesture may start. This gesture recognizer ignores any touches beyond the first touch.
要在自定义的ViewController容器中支持右滑返回,可能就需要用到它。
NavigationControllerr滑动返回的更多相关文章
- iOS 如何设置导航的滑动返回手势, 和系统饿一样
iOS 7 滑动返回那些事儿 2014/05/17 Wei .entry-meta .entry-header 在智能机越来越普及,屏幕越做越大的当下,滑动返回手势已经成为了一个应用的标配功能,甚至可 ...
- iOS 自定义返回按钮,保留系统滑动返回
原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...
- 滑动返回类库SwipeBackLayout的使用问题,解决返回黑屏,和看到桌面
SwipeBackLayout是一个很好的类库,它可以让Android实现类似iOS系统的右滑返回效果,但是很多用户在使用官方提供的Demo会发现,可能出现黑屏或者返回只是看到桌面背景而没有看到上一个 ...
- 类似IOS的滑动返回上一级,SwipeBackLayout-android的滑动返回类库
最近,公司在开发App的需求中增加了一个新的需求,要在android的页面中增加向右滑动的时候返回上一级页面.我刚知道这个需求的时候,感觉有点坑,可能设计那边最近接触到知乎的客户端或者是IOS的滑动可 ...
- UINavigationController侧滑滑动返回 卡死问题
UINavigationController滑动返回,有需要的朋友可以参考下. 最近做了UINavigationController的滑动返回(IOS7及以后系统默认支持的), 主要分成以下几步以及碰 ...
- iOS开发——实用技术OC篇&8行代码教你搞定导航控制器全屏滑动返回效果
8行代码教你搞定导航控制器全屏滑动返回效果 前言 如果自定了导航控制器的自控制器的leftBarButtonItem,可能会引发边缘滑动pop效果的失灵,是由于 self.interactivePop ...
- iOS之手势滑动返回功能-b
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- 禁用ios7 手势滑动返回功能
禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...
- Activity中 左滑动返回监听
网易新闻中有个比较炫的效果,在QQ进入聊天界面也有这种效果,就是从界面左侧滑动到右侧时,界面退出,其实功能很容易实现: 1) Activity 去实现 implements OnTouchListen ...
随机推荐
- Swift & OC 混编 浅析
转载自:http://www.infoq.com/cn/articles/wangyi-cartoon-swift-mixed-practice?utm_campaign=rightbar_v2&am ...
- linux通用邻居基础架构
1.为每一个协议提供一个缓存来存放L3到L2的转换结果. 2.提供在缓存中添加.删除.改变和查找一个特定映射项的函数.查找函数必须要快,因为它会影响整个系统的性能. 3.为每一个协议缓存的数据项提供一 ...
- 为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
在Android硬件抽象层(HAL)概要介绍和学习计划一文中,我们简要介绍了在Android系统为为硬件编写驱动程序的方法.简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的 ...
- 笔记整理--Http-Cookie
如何设置一个永远无法删除的Cookie -- 系统架构 -- IT技术博客大学习 -- 共学习 共进步! - Google Chrome (2013/6/20 9:46:38) 如何设置一个永远无法删 ...
- Windows下MongoDB安装及创建用户名和密码
下载MongoDB的安装文件https://www.mongodb.com/download-center#community,选择合适的版本(注:本人选择的是3.2.6) 下载完MongoDB.ms ...
- 第19章 网络通信----UDP程序设计基础
用户数据报协议(UDP)是网络信息传输的另一种形式. 基于UDP通信的基本模式如下: (1)将数据打包(称为数据包),然后将数据包发往目的地. 发送数据包: 使用DatagramSocket()创建一 ...
- 高性能web开发:如何加载js,,js的存放位置
外部JS的阻塞下载 所有浏览器在下载JS的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.至到JS下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容. 有人会问:为什么JS不能像 ...
- javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)
原文地址:http://javascript.info/tutorial/bubbling-and-capturing 先给出最终的结论: Summary Events first are captu ...
- 【树状数组】 poj 2352
题意:给出n个平面二维坐标,对于每个坐标,如果这个坐标跟(0,0)形成的矩形内包含的点数为 k (包含边界,但不包含坐标本身),那么这个坐标就是 level k.输出level 0 - n-1的点数分 ...
- Hibernate的generator属性
本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. <class name="onlyfun ...