一、场景介绍

  现在大多数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. IE下使用ADODB.Stream实现断点续传

    最近研究了一下IE自带的一些Activex控件,可以比较简单的实现断点续传功能 不过这种方式不推荐,因为安全性较低,而且需要修改客户端注册表,调低ie安全配置 还有就是我比较懒,只打算写个思路和几个关 ...

  2. hdu 1520 (树形DP)

    dp[i][0]表示i不参加 dp[i][1]表示i参加 简单的树形dp #include<stdio.h> #include<string.h> #define N 6100 ...

  3. 曾经的pc端项目踩到的一些兼容性的坑及其解决方案

    曾经公司pc端项目一直最低兼容到IE7,要求和chrome下浏览效果一致,真心坑坏了我和另外一个小伙伴(另一个小伙伴以前也没处理过兼容问题).不过还好,在这里真心感谢鑫哥博客的详解,从底层原理讲到了具 ...

  4. 我眼中的PM

    我眼中的PM 1 我眼中的PM 1.1 人云“一个管理,半个专家”,我说“一个管理,两个专家” 如今,我发现我们不得不面对这样一个现实——角色兼职.我习惯上把项目分为三类:性命攸关的项目(涉及到人身安 ...

  5. shuffle 过程

    Shuffle描述着数据从map task输出到reduce task输入的这段过程(Shuffle的正常意思是洗牌或弄乱). 以下是官网的流程图: 从最基本的要求来说,我们对Shuffle过程的期望 ...

  6. BZOJ 1934: [Shoi2007]Vote 善意的投票 最小割

    1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...

  7. SVN安装笔记

    1.先去以下网址去下载服务器端与客户端的SVN 2.服务端SVN的安装 点击next 点击next 再点击next 点击next 点击install 点击Finish,这样服务器端的SVN就安装好了, ...

  8. android学习日记12--布局管理器

    1.概述 布局管理器的用途: a.可以更好的管理组件; b.通过使用布局管理器,Android应用程序可以做到平台无关性 布局管理器都是ViewGroup的子类,所有可充当容器的父类都是ViewGro ...

  9. C# 墙纸更换程序

    Win7 自带的主题可以实现墙纸更换功能,同时也提供了定时更换功能. 附带效果 淡入淡出 如图 C#编写墙纸更换程序,如果使用Windows Api你会看不到那种 淡入淡出 的效果,它只会很突兀的就换 ...

  10. Input

    Input Basic Input Old Input Files Please note that both Input and Request do NOT sanitize your data, ...