一、场景介绍

  现在大多数APP 都有一个需求,就是隐藏某一个页面的NavigationBar。很多开发者直接   [self.navigationController setNavigationBarHidden:YES] 就万事大吉了。但是如果开发者试着将边缘侧滑返回功能加上之后,细心的同学就会发现,如果我们在一个隐藏NavigationBar的页面和一个显示NavigationBar 的页面通过手势来回切换后,再继续push到更深层的页面,顶部的NavigationBar就会出现错乱的情况。因此好多APP 将侧滑返回直接去掉了,但是这样的话,整个APP 就会显得比较粗糙,体验不佳。其实早已iOS自带了侧滑返回功能,只不过大多数时候由于我们自定义了NavigationBarleftBarButtonItem,导致了转场交互代理interactivePopGestureRecognizer失效了,需要我们重新指定一下代理,具体实现方式,网上其他博客有很详细的解释,在此不再赘述。

二、问题解决

  为了更好的描述问题,我特地定义了两个控制器:1、Viewcontroller1(隐藏了导航栏,导航控制器的根控制器) ;2、Viewcontroller2(需要显示导航控制器,由Viewcontroller1 push 而来)。  那么怎么解决“侧滑返回功能”与“隐藏NavigationBar”共存呢?我的思路是:既然再同一个导航控制器栈内会出问题,那么我将Viewcontroller1和Viewcontroller2放在两个导航控制器里不就可以了吗?但是我们还想需要push的效果怎么办?导航控制器是不允许在push一个新的导航控制器的。只能present出来一个新的导航控制器。因此我想到了自定义一个转场动画,让present出来的效果如同push的效果一样。具体的代码等会会贴出,但是建议大家看一下王巍所写的转场动画这部分知识。

  1)设置侧滑返回

#import <UIKit/UIKit.h>

@protocol ModalViewControllerDelegate <NSObject>

-(void) modalViewControllerDidClickedDismissButton:(UIViewController *)viewController;

@end

@interface BaseNavigationController : UINavigationController<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
//@property(nonatomic,weak)UIPanGestureRecognizer *popPan; //转场代理
@property (nonatomic, weak) id<ModalViewControllerDelegate> transDelegate; @end

#import "BaseNavigationController.h"


@interface BaseNavigationController () @property(nonatomic,weak) UIViewController* currentShowVC; @end @implementation BaseNavigationController - (void)viewDidLoad {
[super viewDidLoad];
//请忽略这段设置UI样式的代码,没什么卵用
[self.navigationBar setBarTintColor:[Theme colorOne]];
[self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[self.navigationBar setTranslucent:NO];

self.view.layer.shadowColor = [UIColor blackColor].CGColor;
self.view.layer.shadowOffset = CGSizeMake(-2, 0);
self.view.layer.shadowOpacity = 0.3;
}
//导航控制器一被创建则设置交互手势代理
-(id)initWithRootViewController:(UIViewController *)rootViewController
{
BaseNavigationController* nvc = [super initWithRootViewController:rootViewController];
self.interactivePopGestureRecognizer.delegate = self;
nvc.delegate = self;
return nvc;
} //这段主要是判断为了帮助手势判断当前响应手势的控制器是不是根视图控制器
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (navigationController.viewControllers.count == 1)
self.currentShowVC = Nil;
else if(animated){
self.currentShowVC = viewController;
}else{
self.currentShowVC = Nil;
}
}

//交互手势代理(告诉手势什么时候需要响应,什么时候不响应)
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return (self.currentShowVC == self.topViewController);
}
return YES;
}

设置侧滑返回我们需要在base类里重新设置代理,并实现转场手势的代理,告诉手势当页面为根视图控制器时候手势不响应,其实以上代码就是实现这么点的功能,仅此而已。

  2)设置转场动画

转场动画我是直接修改了王巍的代码,原文地址在这里 https://onevcat.com/2013/10/vc-transition-in-ios7/ 。以下是我修改后的代码,只是修改了动画部分,使用方法是一致的。

#import "BouncePresentAnimation.h"

@implementation BouncePresentAnimation
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.25f;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// 1. Get controllers from transition context
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // 2. Set init frame for toVC
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
toVC.view.frame = CGRectOffset(finalFrame, screenBounds.size.width, 0); // 3. Add toVC's view to containerView
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toVC.view]; // 4. Do animate now
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
toVC.view.frame = finalFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
} #import "NormalDismissAnimation.h" @implementation NormalDismissAnimation
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.25f;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// 1. Get controllers from transition context
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // 2. Set init frame for fromVC
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGRect initFrame = [transitionContext initialFrameForViewController:fromVC];
CGRect finalFrame = CGRectOffset(initFrame, screenBounds.size.width,0 ); // 3. Add target view to the container, and move it to back.
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toVC.view];
[containerView sendSubviewToBack:toVC.view]; // 4. Do animate now
NSTimeInterval duration = [self transitionDuration:transitionContext]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
fromVC.view.frame = finalFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}

  3)又一个坑

大家将转场动画和侧滑功能加入后会发现,我擦当我在ViewController2中再继续push时,如果侧滑则直接返回到了我们的ViewController1控制器,而不是我们想要的次栈顶控制器。这涉及到了手势之间竞争的问题,也就是说转场的手势取代我们边缘侧滑手势去响应了,因此我们要告诉系统,如果当前手势所在的控制器不是present出来的导航控制器的根控制器的话,转场手势就不需要响应。因此需要修改王大师的一部分代码

#import "SwipeUpInteractiveTransition.h"

@interface SwipeUpInteractiveTransition()<UIGestureRecognizerDelegate>
@property (nonatomic, assign) BOOL shouldComplete;
@property (nonatomic, strong) UIViewController *presentingVC;
@end @implementation SwipeUpInteractiveTransition
-(void)wireToViewController:(UIViewController *)viewController
{
self.presentingVC = viewController;
[self prepareGestureRecognizerInView:viewController.view];
} - (void)prepareGestureRecognizerInView:(UIView*)view {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
gesture.delegate = self;
[view addGestureRecognizer:gesture];
} -(CGFloat)completionSpeed
{
return 1 - self.percentComplete;
} - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
// 1. Mark the interacting flag. Used when supplying it in delegate.
self.interacting = YES;
[self.presentingVC dismissViewControllerAnimated:YES completion:nil];
break;
case UIGestureRecognizerStateChanged: {
// 2. Calculate the percentage of guesture
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat fraction = translation.x / screenSize.width;
//Limit it between 0 and 1
fraction = fminf(fmaxf(fraction, 0.0), 1.0);
self.shouldComplete = (fraction > 0.5); [self updateInteractiveTransition:fraction];
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
// 3. Gesture over. Check if the transition should happen or not
self.interacting = NO;
if (!self.shouldComplete || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
[self cancelInteractiveTransition];
} else {
[self finishInteractiveTransition];
}
break;
}
default:
break;
}
} //如果不是根视图控制器则不响应
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if ([self.presentingVC isKindOfClass:[BaseNavigationController class]]) {
BaseNavigationController *nav = (BaseNavigationController*)self.presentingVC;
if (nav.viewControllers.count >=2) {
return NO;
}
}
return YES;
}

至此侧滑和导航栏的隐藏则完美兼容,希望这篇文章对你有用~

隐藏NavigationBar 带来的坑的更多相关文章

  1. iOS7之后经过滑动返回导航栏隐藏和显示带来的坑(转载)

    iOS7之后经过滑动返回导航栏隐藏和显示带来的坑 Apple 自从iOS7之后增加了屏幕边缘右滑返回交互的支持,再配合UINavigationController的交换动画,pop上一级的操作变的非常 ...

  2. 通过navigationController跳转界面时隐藏navigationBar上的元素

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. 【转】Android 全屏方案(隐藏NavigationBar)

    http://www.07net01.com/2015/04/822292.html 在android4.0及其以上的版本中,出现了一个很屌的东西,叫做Navigation Bar,它和Status ...

  4. 关于在用Swift开发iOS时如何隐藏NavigationBar和TabBar

    举个例子:如果我有一个页面需要进入时同时隐藏NavigationBar和TabBar,那么我就在那个页面的ViewController的代码里加上下面的代码.就可以实现了.接下来告诉大家每一块要注意的 ...

  5. Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar

    场景分析, 为了完全实现沉浸式效果,在进入特定的app后可以将导航栏移除,当退出app后再次将导航栏恢复.(下面将采用发送广播的方式来移除和恢复导航栏) ps:不修改源码的情况下,简单的沉浸式效果实现 ...

  6. MySQL AND 和 OR 联合使用带来的坑

    MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...

  7. MySQL NULL 使用带来的坑

    MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...

  8. 侧滑返回导航栏以及TabBar隐藏和显示带来的坑

    用系统的UINavigationBar时,返回手势重若碰到前一个页面有bar,后一个页面没bar,或者反过来时动画非常难看 如下图:因为首页隐藏了导航栏,在侧滑的时候导航栏竟然提前消失了,这是因为在侧 ...

  9. 隐藏NavigationBar时的一个坑

    http://www.jianshu.com/p/efb960fed457 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: ...

随机推荐

  1. hdoj 2050 折线分割平面

    折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. oracle表数据误删还原

    首先,找到数据删除前的一个时间点. select timestamp_to_scn(to_timestamp('2013-10-12 8:30:00', 'YYYY-MM-DD HH24:MI:SS' ...

  3. OpenCV学习笔记:MAT解析

    在2001年刚刚出现的时候,OpenCV基于 C 语言接口而建.为了在内存(memory)中存放图像,当时采用名为 IplImage 的C语言结构体,时至今日这仍出现在大多数的旧版教程和教学材料.但这 ...

  4. UVa 二分图匹配 Examples

    这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...

  5. ilitek的电容屏驱动程序ilitek_aimvx.c的分析

    本文记录了ili驱动程序. 参考  http://linhui.568.blog.163.com/blog/static/962652682011786352856/ 后记:现在的情况是,在win上是 ...

  6. BootStrap导航栏的使用

    默认的导航栏 创建一个默认的导航栏的步骤如下: 向 <nav> 标签添加 class .navbar..navbar-default. 向上面的元素添加 role="naviga ...

  7. Java循环语句 while

    特点:先判断,后执行 如: public class HelloWorld { public static void main(String[] args) { int i = 1; // 代表 1 ...

  8. android获取mac地址方法

    http://www.cnblogs.com/xioapingguo/p/4037513.html 网上找的,记录一下 public static String getMacAdress(){ Wif ...

  9. MongoDB命令学习

    mongodb不像关系型数据库有很强大的GUI客户端,虽然mongodb也有,但功能和稳定性实在不敢恭维,所以操作mongodb我们大部分 都是用类似cmd命令的方式(mongodb称为shell操作 ...

  10. C++ 构造过程和析构过程

    1.C++构造和析构的过程,类似于穿衣脱衣的过程.穿衣是:先穿内衣,再穿外套.脱衣是:先脱外套,再脱内衣.C++构造过程:首先调用父类构造方法,再调用子类构造方法.C++析构过程:首先调用子类析构方法 ...