? ? ? ? 當(dāng)頁面使用 UITabBarController + UINavigationController 框架的時(shí)候,當(dāng)跳轉(zhuǎn)到詳情頁面的時(shí)候,如果 UITabBar 仍然存在的話就會(huì)造成邏輯混亂,用戶體驗(yàn)也會(huì)下降,因此我們就有一個(gè)在詳情頁將 UITabBar 隱藏的需求,當(dāng)然,在其他的一些情況也可能有隱藏 UITabBar 的需求, 在這里小編為大家介紹三種隱藏 UITabBar 的方法,大家可以根據(jù)詳細(xì)的需求進(jìn)行選擇。
1、第一種:
直接隱藏當(dāng)前頁面的 UITabBar
// 顯示tabBar
self.tabBarController.tabBar.hidden = NO;
// 隱藏tabBar
self.tabBarController.tabBar.hidden = YES;
2、第二種:
? ? ? ? 將 push 到的頁面的 UItabBar 隱藏
// 在push跳轉(zhuǎn)時(shí)隱藏tabBar
UIViewController *vc2 = [UIViewController new];
vc2.hidesBottomBarWhenPushed = YES;
[vc1 pushViewController:vc2 animated:YES];
? ? ? ?該方法在push頁面的時(shí)候使用,有一定的局限性,根據(jù)其名字可以發(fā)現(xiàn),只有在 push跳轉(zhuǎn)的時(shí)候才會(huì)生效,也就是說在 UITabBarController 和 UINavigationController 結(jié)合使用的時(shí)候能用。
這也正是小編子在開篇時(shí)提到的那種情況,小編個(gè)人覺得也是比較常用的一種情況!
3、第三種:
? ? ? ? 不使用系統(tǒng)提供的現(xiàn)有方法,自定義方法,修改 TabBar 的 subview 的 frame 就行了
原理:
? ? ? ? UITabBarController的subview 共有兩個(gè),一個(gè)叫 UITabBar,就是底下的那個(gè) Bar;另一個(gè)叫UITranstionview,就是 Bar 上面的視圖。這兩個(gè) view 下面還有其他的subview,這就不用去管它了。把UITabBar的 y 向下移49個(gè)單位,把UITranstionview 的 hight 加長 49 個(gè)單位。
代碼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;
}
以上是小編總結(jié)的三種方法,也是從各位大神的博客總結(jié)的,如果有什么新的方法,歡迎一起討論!