UITabBarController 隐藏 Bootom Tab Bar

需求:假设有三个页面 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值