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 ...
随机推荐
- Sublime Text 3 搭建 React.js 开发环境
sublime有很强的自定义功能,插件库很庞大,针对新语言插件更新很快,配合使用可以快速搭建适配语言的开发环境. 1. babel 支持ES6, React.js, jsx代码高亮,对 JavaScr ...
- hdu1068 Girls and Boys
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1068 二分图的最大独立集数=节点数(n)— 最大匹配数(m) 另外需要注意的是: 本题求出的最大匹配数是实 ...
- java.util.concurrent包API学习笔记
newFixedThreadPool 创建一个固定大小的线程池. shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭. awaitTermination():用于等待子线程结束, ...
- 546C. Soldier and Cards
题目链接 题意 两个人玩扑克,共n张牌,第一个人k1张,第二个人k2张 给定输入的牌的顺序就是出牌的顺序 每次分别比较两个人牌的第一张,牌上面数字大的赢,把这两张牌给赢的人,并且大的牌放在这个人的牌最 ...
- [iOS]如何给Label或者TextView赋HTML数据
// // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights ...
- RestTemplateIntegrationTests
摘录RestTemplate的集成测试类/* 2. * Copyright 2002-2010 the original author or authors. 3. * 4. * L ...
- 对Memcached使用的总结和使用场景
1.memcached是什么 Memcached 常被用来加速应用程序的处理,在这里,我们将着重于介绍将它部署于应用程序和环境中的最佳实践.这包括应该存储或不应存储哪些.如何处理数据的灵活分布以 及如 ...
- ios7 webapp touch bug
// ios7 touchstart bug if(navigator.userAgent.indexOf("iPhone OS 7") != -1){ var startX = ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- 解决eclipse maven 项目重新下载包这个问题
问题:eclipse项目使用maven下载依赖包,但是有时候断网什么来着就不会自动下载了,挺蛋疼了. 所以,需要我们重新更新项目下载呢. 首先是要在maven的conf文件下setting.xml配置 ...