【原】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;
}

 

以上是小編總結的三種方法,也是從各位大神的博客總結的,如果有什么新的方法,歡迎一起討論!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM