您的位置:首页 > 产品设计 > UI/UE

UITabBarController 隐藏 Bootom Tab Bar

2013-03-09 21:06 344 查看
需求:假设有三个页面 a, b, c, 三个页面可以随意切换,例如 a->b、b->c、c->a、b->a 、c->b 等,并且,我们希望三个页面只初始化一次。

思考:

通常,app会由几个ViewController组成,当由多个ViewController的时候,就需要对其进行管理,负责一个或者多个 ViewController管理并对其生命周期、屏幕旋转进行管理的类称为容器(Container View Controller),通常他们也是一个ViewController,例如UINavigationController、UITabbarController,也有极少数不是ViewController,例如UIPopoverController。

IOS5之后,已经支持自定义的Container View Controller,但对于简单的应用,比如只有三个页面,要去自定义一个 Container View Controller,好像没有什么必要, 于是我们考虑使用默认的一些Container View Controller。

UITabbarController应该最接近我们的需求。我们需要做的,就是将底部导航栏隐藏,并且自定义切换视图即可。

关键代码如下(在TabbarController的子页ViewController中实现)

在m文件中实现两个函数:

/**
*	@brief	隐藏底部导航栏
*/
- (void) HideTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.01];
float fHeight = screenRect.size.height;
if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
fHeight = screenRect.size.width;
}

for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
/**
*	@brief	显示底部导航栏
*/
- (void) ShowTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height - 49.0;
if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width - 49.0;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.01];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
}
}
[UIView commitAnimations];
}


例子: http://download.csdn.net/detail/baijinwen/5125178
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: