[转]UITabBar的隐藏与显示

1.系统方法  self.hidesBottomBarWhenPushed = YES;

该方法存在一定缺陷,根据其名字可以发现,只有在push的时候才会生效,也就是说在UITabBarController和UINavigationController结合使用的时候能用。

2.第二种方法比较通用,原理也很简单。

修改 TabBar 的 subview 的 frame 就行了。其中,UITabBarController的subview 共有两个,一个叫 UITabBar,就是底下的那个 Bar;另一个叫UITranstionview,就是 Bar 上面的视图。这两个 view 下面还有其他的subview,这就不用去管它了。把UITabBar的 y 向下移49个单位,把UITranstionview 的 hight 加长 49 个单位。

 - (void)hidesTabBar:(BOOL)hidden{

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:0];

     for (UIView *view in self.tabBarController.view.subviews) {
         if ([view isKindOfClass:[UITabBar class]]) {
             if (hidden) {
                 [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height, view.frame.size.width , view.frame.size.height)];

             }else{
                 [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height - 49, view.frame.size.width, view.frame.size.height)];

             }
         }else{
             if([view isKindOfClass:NSClassFromString(@"UITransitionView")]){
                 if (hidden) {
                     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height)];

                 }else{
                     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 49 )];

                 }
             }
         }
     }
     [UIView commitAnimations];

 }

Add a Comment

您的电子邮箱地址不会被公开。