iOS左滑手势失效
iOS7之后,苹果优化了一个小功能,就是对于UINavagationController堆栈里的UIViewController,只要轻轻在视图控制器的左边缘右滑一下,该视图控制器就会pop出栈(前提当然是对于非根视图控制器而言)。实现方法很简单,一句话搞定:
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
事实上对于一个视图控制器而言,该属性的默认值即为YES,因此不设置也能实现右滑pop的功能。
然而这个功能很有局限性,因为它不允许当前视图控制器自定义了leftBarButtonItem,一旦自定义,右滑功能就会失效。这里有一个方法:
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
设置代理为nil之后即便自定义了leftBarButtonItem也可以右滑pop。
或者,把手势的许可打开 也可:
self.navigationController.interactivePopGestureRecognizer.enabled = YES ;
事实上如果自定义了leftBarButtonItem,常用的做法是重新设置代理:
- (void)viewDidAppear:(BOOL)animated{
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
然后实现手势协议即可:
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer{
//判断是否为rootViewController
if (self.navigationController && self.navigationController.viewControllers.count == 1) {
return NO;
}
return YES;
}
不过呢,如果我们自定义的返回button只是文字或图片的话,这样设置就可以,不会失效
UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
item.title = @"";
self.navigationItem.backBarButtonItem = item;
如果是要自定义view的当作button的话,就要用leftBarButtonItem设置,并用上述讲的防止手势失效的方案.
有朋友提出以上方式在多次滑动之后会导致界面假死,这里再给出一种解决方案:
在所有除一级页面之外的页面的viewDidAppear和viewWillDisappear中加入以下代码:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//代理置空,否则会闪退
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//开启iOS7的滑动返回效果
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//只有在二级页面生效
if ([self.navigationController.viewControllers count] == 2) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
}
在UINavigationController的delegate中实现以下方法:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//开启滑动手势
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
在pushviewcontroller之前加入以下代码:
//在切换界面的过程中禁止滑动手势,避免界面卡死
if ([_currentNav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
_currentNav.interactivePopGestureRecognizer.enabled = NO;
}
[_currentNav pushViewController:viewController animated:YES];
即可在实现滑动返回的同时,避免界面卡死的问题。
iOS左滑手势失效的更多相关文章
- ionic ios 左滑 白屏
之前发现ionic在发布ios之后,左滑屏幕的时候会出现界面变白,但是画面原有的位置点击还是有效的,但是点击之后界面是不正确的,返回到上上一步 然后查找资料发现是ios系统内置的左滑动作造成了影响,修 ...
- 关于使用navigationController,前后2个视图控制器navigationBar隐藏属性不同,导致右滑手势失效问题的解决办法
###1.问题描述:如A是navigationController的rootViewController,在这个页面navigationBar是显示的(隐藏属性为NO),它push圧栈过来B视图控制器 ...
- iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决
我相信针对每一个iOS开发者来说~除了根视图控制器外~所有的界面通过导航栏push过去的界面都是可以通过右滑来返回上一个界面~其实~在很多应用和APP中~用户已经习惯了这个功能~然而~作为开发者的我们 ...
- [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子
转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...
- iOS UITableView左滑操作功能的实现(iOS8-11)
WeTest 导读 本文主要是介绍下iOS 11系统及iOS 11之前的系统在实现左滑操作功能上的区别,及如何自定义左滑的标题颜色.字体大小. 一.左滑操作功能实现 1.如果左滑的时候只有一个操作按钮 ...
- [Xcode 实际操作]五、使用表格-(9)删除UITableView单元格(手势左滑调出删除按钮)
目录:[Swift]Xcode实际操作 本文将演示如何删除某一行单元格.手势左滑调出删除按钮. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIK ...
- iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除
首先创建项目,在storyboard如下布局控件,设置好约束 然后创建cell模型类XMGWineCell数据模型类XMGWine创建UITableView,设置数据源协议,实现数据源方法懒加载数据这 ...
- 第二十六篇、因为自定item(nav)而使系统右滑返回手势失效的解决方法
@interface ViewController () <uigesturerecognizerdelegate> @end@implementation ViewController ...
- iOS7 NavigationController 右滑手势问题
苹果一直都在人机交互中尽力做到极致,在iOS7中,新增加了一个小小的功能,也就是这个api:self.navigationController.interactivePopGestureRecogni ...
随机推荐
- 【CodeForces 830C】奇怪的降复杂度
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=60638239 description 有n棵竹子 ...
- nginx的proxy_set_header
nginx配置的server段: listen 8888; server_name wyc.com; location /user { proxy_set_header HOST fake.com; ...
- OC语言基础之代码的封装
1.封装的注意点 1: // 成员变量尽量不要用@public 2: // @public 3: int age; 1: //@public 2: // 只读(readonly):只允许外界访问我的n ...
- Bluetooth篇 开发实例之八 匹配
自己写的App匹配蓝牙设备,不需要通过系统设置去连接. 匹配和通信是两回事. 用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK ...
- Spring Boot中使用Feign调用时Hystrix提示异常:"could not be queued for execution and no fallback available."以及"Rejected command because thread-pool queueSize is at rejection threshold"
说明: 1.我还没有真正理解Spring Cloud的精髓,现只停留在使用阶段,可能存在分析不到位的问题. 1.这个是由于线程池的最大数量导致的,官方说随着线程池的数量越大,资源开销也就越大,所以调整 ...
- Word中设置所有西文字体为新罗马
如图所示,不用一个一个设置,在字体里直接设置细纹字体:Times New Roman ,中文字体不用管.
- 在ubuntu12.04中安装wine和source insight
1.安装wine sudo apt-get install wine 2.安装source insight 将source insight安装的可运行文件拷贝到ubuntu中.我拷贝到了~/Deskt ...
- Shell--变量内容的删除、替代与替换
1. 变量内容的删除与替换 #代表由前面开始删除,所以这里便由开始的/删起,*来代替0到无穷多个任意字符 %由后面向前删除变量内容 例如:echo ${path%:*bin}删除最有一个目录,即从:到 ...
- tensorflow BasicRNNCell调试
运行以下代码,进入~/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py和~/anaconda3/lib/python ...
- LINQ获取两个List的交集
1.调用: UserList = UserList.ToList().Intersect(userIDList, new MyUserComparer()).AsQueryable(); 2.须要重写 ...