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 ...
随机推荐
- hadoop聚群的安装
第一部分,安装单机hadoop 1,安装ssh sudo apt-get install ssh 注意:如果执行不了这句,那就先执行:sudo apt-get update 2,安装rsync sud ...
- 让DJANGO里的get_success_url定义的reverse_lazy带参数跳转
按一般的CBVS实现,这个是编辑UPDATEVIEW完成之后,跳到LISTVIEW的. 但如果带跳到DETAILVIEW,则reverse_lazy需要带上参数进行跳转. 实现预定义的PK键跳转代码如 ...
- 天灵灵,地灵灵,但愿这个一定灵!!!python调用win32api,启动应用程序窗口
这个是逼到没办法,C#那一套,一点基本没有. 还好,网上找到例程,可以指定帐户启动进程,但愿可以摆脱WIN SERVICE启动产生的SESSION 0 隔离问题. 因为这个问题,以SERVICE启动的 ...
- BFS 模板
转自:欣哥 下面是bfs一般的形式,谈不上模板但一般都这么来做有点乱有什么多交流 bfs一般用于求最短时间 #include<stdio.h>#include<queue>us ...
- 日志工具logback的简介与配置
Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-c ...
- Sina App Engine(SAE)入门教程(8)- SaeFetchurl使用
fetchurl是什么? FetchURL是SAE为开发者提供的分布式网页抓取服务,用来同步的抓取http页面,FetchURL针对国内的网络的做了优化,内部有调度系统,尽可能保证用户快速的抓取到目标 ...
- Maven基本操作命令
1.mvn package 此命令包含一系列过程:validate-->compile-->test-->package Maven根据pom文件里packaging的配置,决定是生 ...
- STL容器的效率比较
1.介绍 顺序存储容器 : string.vector.list.deque 关联存储容器:map底层采用的是树型结构,多数使用平衡二叉树实现,查找某一值是常数时间,遍历起来效果也不错, 只是每次插入 ...
- Android studio在真机上进行调试
1.在Android Studio中,把app的默认启动目标改为USB device,点击[app]→[app configuration],在[Target Device]选择[USB device ...
- IE css expression(表达式)
很多时候我们需要对IE6的bug写一些hack,如max-height,absolute元素高度100%等. css里面的 expression(表达式)和js里面的差不多,如: 获取当前元素的高度: ...