【原】iOS學習之UITabBar的隱藏,iosuitabbar

來源:互聯網
上載者:User

【原】iOS學習之UITabBar的隱藏,iosuitabbar

 當頁面使用 UITabBarController + UINavigationController 架構的時候,當跳轉到詳情頁面的時候,如果 UITabBar 仍然存在的話就會造成邏輯混亂,使用者體驗也會下降,因此我們就有一個在詳情頁將 UITabBar 隱藏的需求,當然,在其他的一些情況也可能有隱藏 UITabBar 的需求, 在這裡小編為大家介紹三種隱藏 UITabBar 的方法,大家可以根據詳細的需求進行選擇。

1、第一種:

 直接隱藏當前頁面的 UITabBar

// 顯示tabBarself.tabBarController.tabBar.hidden = NO;// 隱藏tabBarself.tabBarController.tabBar.hidden = YES;

2、第二種:

 將 push 到的頁面的 UItabBar 隱藏

// 在push跳轉時隱藏tabBarUIViewController *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;}

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.