隐藏NavigationBar 带来的坑
一、场景介绍
现在大多数APP 都有一个需求,就是隐藏某一个页面的NavigationBar。很多开发者直接 [self.navigationController setNavigationBarHidden:YES] 就万事大吉了。但是如果开发者试着将边缘侧滑返回功能加上之后,细心的同学就会发现,如果我们在一个隐藏NavigationBar的页面和一个显示NavigationBar 的页面通过手势来回切换后,再继续push到更深层的页面,顶部的NavigationBar就会出现错乱的情况。因此好多APP 将侧滑返回直接去掉了,但是这样的话,整个APP 就会显得比较粗糙,体验不佳。其实早已iOS自带了侧滑返回功能,只不过大多数时候由于我们自定义了NavigationBar的leftBarButtonItem,导致了转场交互代理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 带来的坑的更多相关文章
- iOS7之后经过滑动返回导航栏隐藏和显示带来的坑(转载)
iOS7之后经过滑动返回导航栏隐藏和显示带来的坑 Apple 自从iOS7之后增加了屏幕边缘右滑返回交互的支持,再配合UINavigationController的交换动画,pop上一级的操作变的非常 ...
- 通过navigationController跳转界面时隐藏navigationBar上的元素
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 【转】Android 全屏方案(隐藏NavigationBar)
http://www.07net01.com/2015/04/822292.html 在android4.0及其以上的版本中,出现了一个很屌的东西,叫做Navigation Bar,它和Status ...
- 关于在用Swift开发iOS时如何隐藏NavigationBar和TabBar
举个例子:如果我有一个页面需要进入时同时隐藏NavigationBar和TabBar,那么我就在那个页面的ViewController的代码里加上下面的代码.就可以实现了.接下来告诉大家每一块要注意的 ...
- Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
场景分析, 为了完全实现沉浸式效果,在进入特定的app后可以将导航栏移除,当退出app后再次将导航栏恢复.(下面将采用发送广播的方式来移除和恢复导航栏) ps:不修改源码的情况下,简单的沉浸式效果实现 ...
- MySQL AND 和 OR 联合使用带来的坑
MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...
- MySQL NULL 使用带来的坑
MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...
- 侧滑返回导航栏以及TabBar隐藏和显示带来的坑
用系统的UINavigationBar时,返回手势重若碰到前一个页面有bar,后一个页面没bar,或者反过来时动画非常难看 如下图:因为首页隐藏了导航栏,在侧滑的时候导航栏竟然提前消失了,这是因为在侧 ...
- 隐藏NavigationBar时的一个坑
http://www.jianshu.com/p/efb960fed457 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: ...
随机推荐
- Mac OS环境下媒体文件分割工具mediafilesegmenter的简单使用(生成M3U8 TS文件)
mediafilesegmenter是苹果开发的一款用于分割媒体文件的工具,其功能与mediastreamsegmenter相似,但操作更简单. * 具体可以对比博客中的另一篇简介<Mac OS ...
- jquery validation 简单验证手机号码
js代码 // 手机号码验证 jQuery.validator.addMethod("isMobile", function(value, element) { var lengt ...
- eclipse 护眼色
eclipse windows 窗口背景颜色 保护视力 博客分类: 工具使用 EclipseWindows eclipse 窗口背景颜色 windows 窗口背景颜色
- java System 常用方法
一.System.currentTimeMillis() 获取系统当前时间,毫秒 二.System.getProperty Java.version Java 运行时环境版本 java.vendor ...
- 完全分布式Hadoop2.3安装与配置
一.Hadoop基本介绍 Hadoop优点 1.高可靠性:Hadoop按位存储和处理数据 2.高扩展性:Hadoop是在计算机集群中完成计算任务,这个集群可以方便的扩展到几千台 3.高效性:Hadoo ...
- C++ 过载,重写,隐藏
1.过载:在一个类中(也就是一个作用域),方法名相同,形参表不同的方法. 2.重写:父类方法使用virtual,子类方法和方法的方法名,形参表,返回类型相同,子类可以不使用virtual,但是建议使用 ...
- Lazy Load 图片延迟加载(转)
jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...
- JS之正则表达式验证URL
function IsURL(str_url){ var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9 ...
- JAVA获取CLASSPATH路径--转
ClassLoader提供了两个方法用于从装载的类路径中取得资源: public URL getResource(String name); public InputStream ge ...
- 浅谈iOS中的视图优化
引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...