【原】iOS学习之UITabBar的隐藏

 当页面使用 UITabBarController + UINavigationController 框架的时候,当跳转到详情页面的时候,如果 UITabBar 仍然存在的话就会造成逻辑混乱,用户体验也会下降,因此我们就有一个在详情页将 UITabBar 隐藏的需求,当然,在其他的一些情况也可能有隐藏 UITabBar 的需求, 在这里小编为大家介绍三种隐藏 UITabBar 的方法,大家可以根据详细的需求进行选择。

1、第一种:

 直接隐藏当前页面的 UITabBar

// 显示tabBar
self.tabBarController.tabBar.hidden = NO;
// 隐藏tabBar
self.tabBarController.tabBar.hidden = YES;

2、第二种:

 将 push 到的页面的 UItabBar 隐藏

// 在push跳转时隐藏tabBar
UIViewController *vc2 = [UIViewController new];
vc2.hidesBottomBarWhenPushed = YES;
[vc1 pushViewController:vc2 animated:YES];

 该方法在push页面的时候使用,有一定的局限性,根据其名字可以发现,只有在 push跳转的时候才会生效,也就是说在 UITabBarController 和 UINavigationController 结合使用的时候能用。

 这也正是小编子在开篇时提到的那种情况,小编个人觉得也是比较常用的一种情况!

3、第三种:

 不使用系统提供的现有方法,自定义方法,修改 TabBar 的 subview 的 frame 就行了

 原理:

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

 代码1:

- (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];
     
 }

 代码2:

-(void)makeTabBarHidden:(BOOL)hide { // Custom code to hide TabBar
    if ( [self.tabBarController.view.subviews count] < 2 )
    {
        return;
    }
    UIView *contentView; if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
    {
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    } else {
            contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    }
    if (hide) {
        contentView.frame = self.tabBarController.view.bounds;
    } else {
            contentView.frame = CGRectMake(self.tabBarController.view.bounds.origin.x, self.tabBarController.view.bounds.origin.y,
                                           self.tabBarController.view.bounds.size.width, self.tabBarController.view.bounds.size.height -
                                           self.tabBarController.tabBar.frame.size.height);
    }
    self.tabBarController.tabBar.hidden = hide;
}

 

以上是小编总结的三种方法,也是从各位大神的博客总结的,如果有什么新的方法,欢迎一起讨论!

posted @ 2016-06-17 22:50  墨隐于非  阅读(10833)  评论(1编辑  收藏  举报