一、前言
項(xiàng)目中在跳轉(zhuǎn)子頁(yè)面的時(shí)候隱藏tabbar是個(gè)很常見(jiàn)的需求,蘋(píng)果也提供了方便的方法,即設(shè)置控制器的hidesBottomBarWhenPushed屬性,但設(shè)置錯(cuò)誤,就會(huì)出現(xiàn)莫名其妙的問(wèn)題,曾經(jīng)就掉入過(guò)坑中直到抓狂??
二、hidesBottomBarWhenPushed作用
A view controller added as a child of a navigation controller can display an optional toolbar at the bottom of the screen. The value of this property on the topmost view controller determines whether the toolbar is visible. If the value of this property is YES, the toolbar is hidden. If the value of this property is NO, the bar is visible.
??????上面是蘋(píng)果對(duì)屬性hidesBottomBarWhenPushed的解釋:??????
大意是:已經(jīng)添加到導(dǎo)航控制器的子控制器可選擇性的展示屏幕底部的toolbar。 最頂部的子控制器的屬性值(hidesBottomBarWhenPushed)決定toolbar是否可見(jiàn),如果屬性值為YES,toolbar隱藏,為NO,則可見(jiàn)。
三、例子:A ->B,A push 顯示B,push時(shí)B底部的tabor隱藏

1. 曾經(jīng)錯(cuò)誤的設(shè)置(以下的設(shè)置都是在A控制器中)
-
設(shè)置:在pushB之前,使 A.hidesBottomBarWhenPushed = Yes;
// A push B - (IBAction)nextPage:(id)sender // push之前設(shè)置A的hidesBottomBarWhenPushed屬性 BViewController *BVC = [[BViewController alloc] init]; self.hidesBottomBarWhenPushed = YES; //self為A控制器 [self.navigationController pushViewController:BVC animated:YES]; } -
結(jié)果:
在點(diǎn)擊next的時(shí)候,確實(shí)成功跳轉(zhuǎn)并隱藏了tabbar,滿意??;
可返回的以后,tabbar隱藏了,這不是我想要的結(jié)果??;
-
那解決問(wèn)題唄,在viewWillAppear的時(shí)候設(shè)置??
// 解決返回時(shí)tabbar的隱藏問(wèn)題 - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.tabBarController.tabBar.hidden = NO; self.hidesBottomBarWhenPushed = NO; } 設(shè)置后算是比較符合需求了,雖然效果怪怪的。本以為算是成功了,可再次push時(shí)又出問(wèn)題了????????????
2. 正確做法
?????? 查查查資料。。。算是理解了,再看看蘋(píng)果解釋,有兩個(gè)關(guān)鍵點(diǎn):
1.已經(jīng)添加到導(dǎo)航控制器的子控制器;
2.最頂部的控制器;
-
錯(cuò)誤原因
- 系統(tǒng)在push B的時(shí)候,B控制器其實(shí)已經(jīng)加入到導(dǎo)航控制器的子控制器中了;
- 此時(shí),B才是最頂部的控制器,B的hidesBottomBarWhenPushed屬性才是正確控制tabbar隱藏的關(guān)鍵,而不是A的;
-
既然找到原因,那就改:
-
清除先前的相關(guān)設(shè)置
// 去除先前的設(shè)置,例如下面的 A.hidesBottomBarWhenPushed = Yes; A.tabBarController.tabBar.hidden = NO; A.hidesBottomBarWhenPushed = NO; -
正確設(shè)置:
/// A push B - (IBAction)nextPage:(id)sender { // 設(shè)置B的hidesBottomBarWhenPushe BViewController *BVC = [[BViewController alloc] init]; BVC.hidesBottomBarWhenPushed = YES; // 設(shè)置B [self.navigationController pushViewController:BVC animated:YES]; } 結(jié)果: 運(yùn)行,多次測(cè)試,無(wú)誤,放心了。??
上面的設(shè)置只需要在NavigationController的根控制器push到其他頁(yè)面的時(shí)候設(shè)置一下就好,其他子頁(yè)面都會(huì)隱藏tabbar,不用再在其他頁(yè)面設(shè)置。
-
-
其實(shí)還可以整體設(shè)置下,重寫(xiě)UINavigationController中的push方法,免得每個(gè)tab下的控制器都設(shè)置。
// 重寫(xiě)自定義的UINavigationController中的push方法 // 處理tabbar的顯示隱藏 -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.childViewControllers.count==1) { viewController.hidesBottomBarWhenPushed = YES; //viewController是將要被push的控制器 } [super pushViewController:viewController animated:animated]; }