隐藏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: ...
随机推荐
- JavaScript要点(十) HTML DOM - 改变 HTML
HTML DOM 允许 JavaScript 改变 HTML 元素的内容. A.改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Thu Oct 13 2 ...
- android应用程序fps meter[帧数显示]的分析 —— 浅谈root的风险 (3)
上节已经详细说了下注入过程,最后寄生进程在宿主进程中下了个蛋,这下完的蛋有什么作用呢?接下来再具体分析一下. lib0的感染过程分析 对于本例注入的so动态库,首先看一下so的符号: $ readel ...
- JAVA使用EPoll来进行NIO处理的方法(转)
JDK 6.0 以及JDK 5.0 update 9 的 nio支持epoll (仅限 Linux 系统 ),对并发idle connection会有大幅度的性能提升,这就是很多网络服务器应用程序需要 ...
- PostgreSQL的prepare 和 execute 动作背后
我给PostgreSQL的源代码加入了调试信息以后,会有如下表现: 我执行Prepare: postgres=# prepare s(; PREPARE postgres=# 背后的反应: ** In ...
- 中国大概能用的NTPserver地址
133.100.11.8 prefer210.72.145.44203.117.180.36131.107.1.10time.asia.apple.com64.236.96.53130.149.17. ...
- java nio 抛出NonWritableChannelException异常
抛出异常的代码在此处: MappedByteBuffer buffer = channel.map(MapMode.READ_WRITE, 0, avalible); 其中channel是一个file ...
- Android手机上判断网络运营商
我们想获取手机的运营商信息.通常都会去调用系统的TelephonyManager类的取数据.但是很多时候可能取不到卡的信息(例如双卡手机和 一些特殊卡),这样就区别不了运营商了.但是有时候我们的需求要 ...
- uboot中gd的定义和使用
近期在做uboot中nand启动相关的工作,遇到一个问题一直纠结着.如今最终明确了这个问题,想想还有好多兄弟在某个黑暗的角落里或者某台电脑前纠结着呢,所以赶紧写下来以供查阅. uboot versio ...
- firefly的rpc。。
firefly使用了twisted的pb 来实现rpc: http://twistedmatrix.com/documents/current/core/howto/pb-usage.html 服务端 ...
- [Effective C++ --020]宁以pass-by-reference-to-const替换pass-by-value
前言: 我们都用过C的值传递方式,那么在C++情况下,值传递是怎样工作的呢? 比如: int foo(int x); int i; foo(i); 1.程序内部先取得i的一个副本 2.将副本传递到fo ...