您的位置:首页 > 其它

tabBar隐藏动画,hidesBottombarWhenPushed

2016-03-23 11:19 579 查看
在UITabbarController包含的UINavigationController应用中,如果UINavigationController某一页(某个level)需要隐藏Tabbar,之前的做法是在push那一页之前,将那一页的ViewController中的hidesBottombarWhenPushed参数设为YES,这样当那一页push进UINavigationController中时,底部的Tabbar就会隐藏掉。

但是这种方法有一个潜在的而又巨大的问题,假设现在我有3个ViewController A,B,C,其中为Navigation的RootViewController,A中push B,B中push C。这时,如果我想要在B中隐藏Tabbar,而在C中显示Tabbar,根据最朴素的想法就是:

//SecondLevel即为B,此代码在A中进行
SecondLevelViewController *_2vc = [[SecondLevelViewController alloc]initWithNibName:@"SecondLevelViewController" bundle:nil];
_2vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:_2vc animated:YES];

//ThirdLevel即为C,此代码在B中进行
ThirdLevelViewController *_3vc = [[ThirdLevelViewController alloc]initWithNibName:@"ThirdLevelViewController" bundle:nil];
_3vc.hidesBottomBarWhenPushed = NO;
[self.navigationController pushViewController:_3vc animated:YES];


然而很可惜,虽然A中push B能让Tabbar隐藏,但是B中push C,就没办法让Tabbar显示出来了。这是一个比较郁闷的问题,我只能猜想其中的原理是,当设置某一层hidesBottomBarWhenPushed = YES之后,UITabbarController就像不存在一样,就连调用self.tabbarController或者self.navigationController.tabbarController也应该是nil。

所以解决办法只能是手动隐藏Tabbar。

- (void) hideTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, HEIGHTIPHONE, view.frame.size.width, view.frame.size.height)];
}
else
{

[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, HEIGHTIPHONE)];
}
}
[UIView commitAnimations];
}

- (void) showTabBar:(UITabBarController *) tabbarcontroller {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{

if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, HEIGHTIPHONE-view.frame.size.height, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, HEIGHTIPHONE-49)];
}
}

[UIView commitAnimations];
}


-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (_isPush) {

[self showTabBar:self.tabBarController];

}
_isPush = NO;
}


- (void)viewWillAppear:(BOOL)animated  {

[super viewWillAppear:animated];
[self hideTabBar:self.tabBarController];

}


//代码中用到的宏:

#define HEIGHTIPHONE [UIScreen mainScreen].bounds.size.height

我的个人观点:

首先对作者表示感谢,很给力,给我门找到了一个平滑的方法,看着很流畅

1 、 在做显示动画的时候我建议写在viewDidAppear里面,这样看着舒服。

2、 两个方法分开,需要隐藏的视图光写隐藏就好了,

3 、 有的时候不需要显示动画,比如再分栏来回切换的时候,tabBar不需要做显示动画,只有从二级界面返回的时候需要,这样我们可以设置一个属性

@property (nonatomic ,assign)BOOL isPush;// 是否推出了二级界面
当push的时候设置成yes,pop回来做完动画,就设置成no。

原文地址:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: