iOS 7 自定义Back按钮 与 Pop interactive gesture 问题
1、自定义Back按钮
iOS中很多时候我们都会自定义返回按钮,也是一件easy的事,类似如下:
// 返回按钮
1 - (void)showNavBackButton
{
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[backButton setBackgroundImage:[UIImage imageNamed:@"00_back_button"]
forState:UIControlStateNormal];
[backButton setTitle:kLoc(@"Back") forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(, , , )];
backButton.titleLabel.font = kMediumFont();
backButton.frame = CGRectMake(, , , );
self.navigationItem.leftBarButtonItem
= [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
但是,这样在iOS7下Pop interactive gesture就不好使了。
这里 Here 有一个解决方法。
但是,测试发现在栈中推入一个controller后,快速向左平滑,将会引起崩溃。
查看崩溃日志,发现如下信息:
nested pop animation can result in corrupted navigation bar
2、解决Pop interactive gesture问题
优化的解决方案是简单的让NavigationController自己成为响应的接受者,最好用一个UINavigationController的子类。
1)在过渡的时候禁用interactivePopGestureRecognizer
2)当新的视图控制器加载完成后再启用,建议使用UINavigationController的子类操作
// 自定义NavigationController
1 @interface DCBaseNavigationController ()
<
UINavigationControllerDelegate,
UIGestureRecognizerDelegate
>
@end @implementation DCBaseNavigationController - (void)dealloc
{
self.interactivePopGestureRecognizer.delegate = nil;
self.delegate = nil; [super dealloc];
} #pragma mark - View lifecycle - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.delegate = self;
self.delegate = self;
}
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Override - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// Hijack the push method to disable the gesture
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
} [super pushViewController:viewController animated:animated];
} #pragma mark - UINavigationControllerDelegate - (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
} @end
3、Pop interactive gesture冲突,造成页面假死问题
我遇到的情况是,Push/Pop页面时,没有立即得到想要的效果,页面没有显出出来,NavigationController的didShowViewController:回调方法也没有调用。
页面布局情况是这样的:视图A,有一个Pan手势;视图B是TabBarController,其ViewControllers都是NavigationController。视图B是视图A的子视图。
后来找到原因是:navigationController的interactive pop手势与视图A的pan手势冲突。
具体原因是:rootViewController加载时,调用了didShowViewController:,设置interactivePopGestureRecognizer可用,其实我们并不需要在root的时候也触发这个手势。所以稍加优化如下:
// 优化
1 - (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//if ([[navigationController.viewControllers firstObject] isEqual:viewController]) {
if ([navigationController.viewControllers count] == ) {
// Disable the interactive pop gesture in the rootViewController of navigationController
navigationController.interactivePopGestureRecognizer.enabled = NO;
} else {
// Enable the interactive pop gesture
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
}
当前显示的是root时,设置interactivePopGestureRecognizer不可用,非root时设置interactivePopGestureRecognizer可用。
参考文章:http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/
iOS 7 自定义Back按钮 与 Pop interactive gesture 问题的更多相关文章
- iOS7自定义back按钮和pop交互手势
Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个le ...
- iOS tableView自定义删除按钮
// 自定义左滑显示编辑按钮 - (NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActio ...
- iOS 自定义返回按钮,保留系统滑动返回
原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...
- (ios实战) UINavigationBar 返回按钮 文本自定义实现
在实际开发过程, 我们使用navigationController时,上一个标题过长,导致下一个界面的返回按钮文本过长,比较难看,如果标题取名过短,又不能完全表达含义. 下面时如何实现返回按钮的Tit ...
- OC导航栏自定义返回按钮
[iOS]让我们一次性解决导航栏的所有问题 在默认情况下,导航栏返回按钮长这个样子 导航栏默认返回按钮 导航栏左上角的返回按钮,其文本默认为上一个ViewController的标题,如果上一个Vi ...
- android自定义控件(3)-自定义当前按钮属性
那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...
- flutter 隐藏返回按钮 自定义返回按钮
自定义返回按钮 //改变颜色 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: BackBu ...
- iOS 如何自定义UISearchBar 中textField的高度
iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...
- iOS 隐藏自定义tabbar
iOS 隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...
随机推荐
- C#图片上写文字
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- POJ2965The Pilots Brothers' refrigerator
http://poj.org/problem?id=2965 这个题的话,一开始也不会做,旁边的人说用BFS,后来去网上看了众大神的思路,瞬间觉得用BFS挺简单易:因为要让一个“+”变为“-”,只要将 ...
- SDUT2482二叉排序树
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2482&cid=1184 题目描述 二叉排序树的定义是:或者是一棵空树,或者是具有下列性质 ...
- SDUT1466双向队列
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1466&cid=1182 题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列 ...
- Hadoop-eclipse-plugin插件安装
Hadoop-eclipse-plugin插件安装 学习Hadoop有一段时间了,以前每次的做法都是先在win下用eclipse写好Mapreduce程序,然后打成jar文件,上传到linux下用ha ...
- wireshark: there are no interfaces on which a capture can be done
权限问题,简单的直接sudo就行. 更安全的做法是: # chmod 4755 /usr/bin/dumpcap dumpcap的所在目录可用whereis命令查看.
- VA对于开发QT是神器
我怎么就忘了,VA也可以适用于VS下开发QT程序.其中QT的头文件自己增加,主要是: C:\Qt\4.8.6_2008\include 但还有一些特殊类不认识,所以还得继续增加: C:\Qt\4.8. ...
- Android:单元测试
通过单元测试的方法可以轻松判断BUG 第一步:首先在AndroidManifest.xml中加入下面红色代码: 打开AndroidManifest.xml,选择instrumentation ,选择N ...
- java:静态成员变量和静态函数
静态成员变量 可以使用类名调用,如 class Dog { static int age; } class Test2{ public static void main(String args[]){ ...
- 在PowerDesigner中设计物理模型1——表和主外键
原文:在PowerDesigner中设计物理模型1--表和主外键 在PD中建立物理模型由以下几种办法: 直接新建物理模型. 设计好概念模型,然后由概念模型生成物理模型. 设计好逻辑模型,然后由逻辑模型 ...