在vcA中顯示tabBar,在vcB中隱藏tabBar。相似這種例子的需求很多。自我總結一下哈哈哈。bash
谷歌下後找到了以下幾種方法:app
首先想從這個簡單的屬性設置講起,由於實際狀況中我也會直接這麼作,可是這麼作帶來的後果是以下:iview
在須要隱藏的頁面來上那麼一句 _tabBarController.tabBar.hidden = YES;ide
tabBar的確隱藏了,可是tabBar的位置會出現一部分空缺,以下圖ui
[self.view addSubview:subView];
this
抑或是改變_view的大小,這部分的空缺仍然沒法填補。spa
不隱藏以前的樣子 code
沒有提供實際有效的方法,反而帶來了個坑。所以這部份內容也只能做爲‘讓讀者少走彎路’的內容看看哈哈哈哈。cdn
對於這個方法,蘋果的官方文檔是:對象
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 true, the toolbar is hidden. If the value of this property is false, the bar is visible.
tabBar的隱藏取決於navigation的棧頂控制器。 我看到網上有以下寫法
@implementation SecondViewController
// 在要push進棧的控制器中重寫該方法
- (BOOL)hidesBottomBarWhenPushed {
return (self.navigationController.topViewController==self);
}
@end
複製代碼
這個方法會在 -(void)pushViewController:animated:;
以後調用。所以,這時候secondVc進棧,等式成立,secondVc中的導航欄被隱藏。可是若是繼續將thirdVc push進棧(不對thirdVc的hidesBottomBarWhenPushed進行重寫),會發現,thirdVc的tabBar重新出現。 緣由是: 在secondVc中執行了[pushviewController:animated]'
以後,secondVc的hidesBottomBarWhenPushed
依舊會被觸發(能夠打斷點試試)。而此時thirdVc進入棧成爲棧頂控制器,等式不成立,故而tabBar復現。
如何隱藏thridVc的tabBar呢?能夠像secondVc同樣,對該方法再次重寫。 而在之後的開發中,針對與任何一個須要隱藏起tabBar的viewController,必要時候仍是須要重寫該方法,避免給本身挖坑。
一直重寫以爲有點麻煩,也能夠集中寫在本身的navViewController中的
-(BOOL)hidesBottomBarWhenPushed;
這類方法和上述的第二種方法修改的對象也是一致的。 可是修改的時候須要注意以下幾個問題
此時secondVc在push的時候就會隱藏tabBar,同時pop出來時候,tabBar依舊顯現。然而在thirdVC push進棧時候,若是想要thirdVc的tabBar顯示,此時經過
thirdVc.hidesBottomBarWhenPushed = NO
是沒法讓thirdVc的tabBar顯現出來的。至於爲何,官方文檔好像也沒有解釋清楚:
If the value of this property is true, the toolbar is hidden. If the value of this property is false, the bar is visible.
卻是在stackoverflow找到了網友解答(以下):
若是在push secondVc進棧時候,寫入 self.hidesBottomBarWhenPushed = YES;
會致使pop回firstVc 時候,tabBar不能出現.
實際狀況中,若是真有必要使得thirdVc的tabBar顯示出來,能夠採用下面的寫法
// 在push secondVc 的時候
SecondVc *secondVc = [SecondVc new];
secondVc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:secondVc animated:YES];
secondVc.hidesBottomBarWhenPushed = NO ;
複製代碼
採用這種肉夾饃的寫法,在push thirdVc進棧以後其tabBar依舊堅挺!😂
補充一些tabBar的小技巧: 設置tabBarItem的imageInset屬性能夠對tabBar內部的寬距進行設置。