iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头.

在一开始写项目的时候,就要做好一个准备,导航栏是自定义还是使用系统的,后期有什么改动,有什么比较特殊的需求、当然这些在更改需求的同时,很多东西都已经被改得面目全非了。

完全自定义导航栏,在实际开发中,并不能满足特殊需求,因此更多情况下,还是需要配合系统导航栏自定义,从而达到我们想要的效果。当我们自定义返回按钮之后,就会出现系统的右滑Pop功能就失效了,这是其中的一个小问题,下面就跟大家分享一下我所了解到的:

实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个leftBarButtonItem.

 文/煜寒了(简书作者)
原文链接:http://www.jianshu.com/p/349636eb3fca
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。 - (void)viewDidLoad
{
self.navigationItem.leftBarButtonItem = [self backButton];
} - (UIBarButtonItem *)backButton
{
UIImage *image = [UIImage imageNamed:@"back_button"];
CGRect buttonFrame = CGRectMake(, , image.size.width, image.size.height); UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal]; UIBarButtonItem *item; = [[UIBarButtonItem alloc] initWithCustomView:button]; return item;
}
但是这样在iOS7上 pop手势交互就不好使了.我发现了一个轻松解决的办法.通过我的beta测试者,我收到了很多关于pop手势的崩溃日志.我发现在栈中推入一个controller后,快速向左平滑,将会引起崩溃.换句话说,如果用户在推入还在进行的时候立即去点击返回.那么导航控制器就秀逗了.我在调试日志里面发现这些:
nested pop animation can result in corrupted navigation bar
经过几个小时的奋斗和尝试,我发现可以缓解这个错误:设置手势的delegate为这个导航控制器就像Stuart Hall在他的帖子说的那样,分配了一个手势交互行为的委托在自定义按钮显示的时候.然后,当用户快速点击退出的时候,控制器因为手势发送了一个消息在本身已经被销毁的时候.我的解决方案是简单的让NavigationController自己成为响应的接受者.最好用一个UINavigationController的子类.
 文/煜寒了(简书作者)
原文链接:http://www.jianshu.com/p/349636eb3fca
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。 @interface CBNavigationController : UINavigationController <UIGestureRecognizerDelegate>
@end @implementation CBNavigationController - (void)viewDidLoad
{
__weak CBNavigationController *weakSelf = self; if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
}
} @end
在转场/过渡的时候禁用 interactivePopGestureRecognizer当用户在转场的时候触发一个后退手势,则各种事件又凑一块了.导航栈内又成了混乱的.我的解决办法是,转场效果的过程中禁用手势识别,当新的视图控制器加载完成后再启用.再次建议使用UINavigationController的子类操作.
 文/煜寒了(简书作者)
原文链接:http://www.jianshu.com/p/349636eb3fca
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。 @interface CBNavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end @implementation CBNavigationController - (void)viewDidLoad
{
__weak CBNavigationController *weakSelf = self; if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
} // Hijack the push method to disable the gesture - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
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
{
// Enable the gesture again once the new controller is shown if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = YES;
} @end
2015-07-21 更新 解决左滑手势冲突和不灵敏的问题
 文/煜寒了(简书作者)
原文链接:http://www.jianshu.com/p/349636eb3fca
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。 -(UIViewController *)popViewControllerAnimated:(BOOL)animated { return [super popViewControllerAnimated:YES];
} #pragma mark UINavigationControllerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([self.childViewControllers count] == ) {
return NO;
}
return YES;
} // 我们差不多能猜到是因为手势冲突导致的,那我们就先让 ViewController 同时接受多个手势吧。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
//运行试一试,两个问题同时解决,不过又发现了新问题,手指在滑动的时候,被 pop 的 ViewController 中的 UIScrollView 会跟着一起滚动,这个效果看起来就很怪(知乎日报现在就是这样的效果),而且也不是原始的滑动返回应有的效果,那么就让我们继续用代码来解决吧
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
} -(UIViewController *)popViewControllerAnimated:(BOOL)animated { return [super popViewControllerAnimated:YES];
} #pragma mark UINavigationControllerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([self.childViewControllers count] == ) {
return NO;
}
return YES;
} // 我们差不多能猜到是因为手势冲突导致的,那我们就先让 ViewController 同时接受多个手势吧。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
//运行试一试,两个问题同时解决,不过又发现了新问题,手指在滑动的时候,被 pop 的 ViewController 中的 UIScrollView 会跟着一起滚动,这个效果看起来就很怪(知乎日报现在就是这样的效果),而且也不是原始的滑动返回应有的效果,那么就让我们继续用代码来解决吧
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}

iOS-自定义导航栏后侧滑返回功能失效的更多相关文章

  1. iOS 自定义导航栏笔记

    一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC ...

  2. ios 自定义导航栏,开启侧滑返回手势

    自定义一个常用ListViewController .h文件 #import <UIKit/UIKit.h> @interface ListViewController : UIViewC ...

  3. ios之自定义导航栏上的返回按钮

    导航栏的按钮,右边的按钮是可以自己随意添加的.但左边的返回按钮怎么定制?你会说,添加一个自己的按钮呗!你可以试试看,这样行不行. 正确的答案是重载UINavigationController类的pus ...

  4. iOS 自定义导航栏 和状态栏

    一.更改状态栏颜色 (StatusBar) 就是比如导航栏是红色的状态栏是绿色的. 要实现这样的效果其实很简单,就是添加一个背景view. 简单的实现过程如下: 1 // 设置导航颜色 可用 2 [s ...

  5. 第十三篇、Swift_Nav自定义返回按钮后或者隐藏导航栏,Pop返回手势失效的解决方法 Pop全局返回添加的方法

    边缘的pop返回手势: override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.purple ...

  6. iOS 隐藏导航栏后,UITableView向下偏移状态栏高度

    if (@available(iOS 11.0, *)) { self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewConte ...

  7. IOS 自定义导航栏背景

    //- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AV ...

  8. iOS 自定义导航栏

    参考链接:https://blog.csdn.net/huanglinxiao/article/details/100537988 demo下载地址:https://github.com/huangx ...

  9. 微信小程序自定义导航栏组件,完美适配所有手机,可实现各种功能和情况

    背景 在做小程序时,关于默认导航栏,我们遇到了以下的问题: Android.IOS 手机对于页面 title 的展示不一致,安卓 title 的显示不居中 页面的 title 只支持纯文本级别的样式控 ...

随机推荐

  1. dom 拖拽回放

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. dom 绘制正方形

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 消除QQ表情小游戏

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. forms

    http://www.cnblogs.com/bomo/p/3309766.html http://www.cnblogs.com/leonwang/archive/2013/03/05/294457 ...

  5. 1) data-options

    <select class="easyui-combobox" data-options="editable:false"> <select ...

  6. hdoj 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...

  7. python 自定义函数

    200 ? "200px" : this.width)!important;} --> 介绍 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参 ...

  8. 网页打印时设置A4大小

    最近开发项目时遇到了网页打印的问题,这是问题之二,打印宽度设置 在公制长度单位与屏幕分辨率进行换算时,必须用到一个DPI(Dot Per Inch)指标. 经过我仔细的测试,发现了网页打印中,默认采用 ...

  9. mac 开发必备软件(不断update ing...)

    整理下mac环境下, 开发必备的一些软件吧, 由于不断要更新ing, 用到啥就写啥~球轻拍 1.host 绑定切换神器 a.gas mask : 只能切换单个自定义的host文件 b.ihosts(推 ...

  10. 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中

    http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...