Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头.
实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个leftBarButtonItem.
- (void)viewDidLoad 2 { 3 self.navigationItem.leftBarButtonItem = [self backButton]; 4 } 5 6 - (UIBarButtonItem *)backButton 7 { 8 UIImage *image = [UIImage imageNamed:@"back_button"]; 9 CGRect buttonFrame = CGRectMake(0, 0, image.size.width, image.size.height); 10 11 UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame]; 12 [button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 13 [button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal]; 14 15 UIBarButtonItem *item; = [[UIBarButtonItem alloc] initWithCustomView:button]; 16 17 return item; 18 }
但是这样在iOS7上 pop手势交互就不好使了.我发现了一个轻松解决的办法.
通过我的beta测试者,我收到了很多关于pop手势的崩溃日志.
我发现在栈中推入一个controller后,快速向左平滑,将会引起崩溃.
换句话说,如果用户在推入还在进行的时候立即去点击返回.那么导航控制器就秀逗了.
我在调试日志里面发现这些:
nested pop animation can result in corrupted navigation bar
经过几个小时的奋斗和尝试,我发现可以缓解这个错误:
设置手势的delegate为这个导航控制器
就像Stuart Hall在他的帖子说的那样,分配了一个手势交互行为的委托在自定义按钮显示的时候.然后,当用户快速点击退出的时候,控制器因为手势发送了一个消息在本身已经被销毁的时候.
我的解决方案是简单的让NavigationController自己成为响应的接受者.最好用一个UINavigationController的子类.
@interface CBNavigationController : UINavigationController <UIGestureRecognizerDelegate> 2 @end 3 4 @implementation CBNavigationController 5 6 - (void)viewDidLoad 7 { 8 __weak CBNavigationController *weakSelf = self; 9 10 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 11 { 12 self.interactivePopGestureRecognizer.delegate = weakSelf; 13 } 14 } 15 16 @end
在转场/过渡的时候禁用 interactivePopGestureRecognizer
当用户在转场的时候触发一个后退手势,则各种事件又凑一块了.导航栈内又成了混乱的.我的解决办法是,转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用.再次建议使用UINavigationController的子类操作.
@interface CBNavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate> 2 @end 3 4 @implementation CBNavigationController 5 6 - (void)viewDidLoad 7 { 8 __weak CBNavigationController *weakSelf = self; 9 10 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 11 { 12 self.interactivePopGestureRecognizer.delegate = weakSelf; 13 self.delegate = weakSelf; 14 } 15 } 16 17 // Hijack the push method to disable the gesture 18 19 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 20 { 21 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 22 self.interactivePopGestureRecognizer.enabled = NO; 23 24 [super pushViewController:viewController animated:animated]; 25 } 26 27 #pragma mark UINavigationControllerDelegate 28 29 - (void)navigationController:(UINavigationController *)navigationController 30 didShowViewController:(UIViewController *)viewController 31 animated:(BOOL)animate 32 { 33 // Enable the gesture again once the new controller is shown 34 35 if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) 36 self.interactivePopGestureRecognizer.enabled = YES; 37 } 38 39 40 @end
转载自: http://www.cnblogs.com/yingkong1987/archive/2013/10/10/3362289.html
相关推荐
ios自定义各种按钮
iOS cell自定义侧滑功能按钮的一个简单的例子,在本人机子上测试通过,想要就拿去。
ios地图自定义标注和气泡
iOS 自定义按钮 很漂亮
在textview或者textfield键盘上添加收回按钮
ios7 自定义UIAlertView ios7 UIAlertView 弹出 警告 自定义 简单实用。 支持转屏
实际开发中经常需要变换按钮的图标和文本的位置,因此本菜鸟封装了一个按钮 对外提供一个枚举属性,外界通过赋值相应的枚举值,创建不同类型的按钮暂时提供6种类型 1.默认状态,2.水平左对齐(图标文本位置交换...
iOS 自定义按钮和弹出框以及点击位置判断
ios不完全自定义的tabar 中间按钮突出,可以切换页面 ,
ios7自定义UITableViewCell
tableview 自定义左滑删除,带圆角 代码很简单 效果看图。没太多复杂东西。
因业务需要需要将cell侧滑按钮多个竖向排布,使用系统代理基本上横向排布,所以捣鼓了个自定义cell竖向排布侧滑多个按钮。 使用说明:1.直接继承基类cell,不影响你的业务UI。仅仅只是具有了cell侧滑功能
自定义双向slider双向滑动实现文字区间选择并显示
ios自定义的segement,自带下滑线,点击按钮会有颜色变化
根据standford的ios7讲义第7课知识,自己写的IOS自定义view描绘纸牌和手势处理的一个例子
iOS 自定义UINavigationController和UITabBarController
自定义的背景颜色,自带控制状态选择
ios列表自定义,cell重用和自定义
简单、优雅地解决 iOS 11 导航栏自定义按钮布局和触摸事件响应的问题
主要介绍了iOS 自定义返回按钮,保留系统滑动返回功能,实现方法非常简单,具有参考借鉴价值,需要的朋友参考下吧