hideBottomBarWhenPushed 使用


1、tabBar隱藏顯示

在vcA中顯示tabBar,在vcB中隱藏tabBar。相似這種例子的需求很多。自我總結一下哈哈哈。bash

谷歌下後找到了以下幾種方法:app

  • 1._tabBarController.tabBar.hidden = YES;
  • 2.重寫hidesBottomBarWhenPushed
  • 3.self.hidesBottomBarWhenPushed = YES;

1._tabBar.hidden = YES;

首先想從這個簡單的屬性設置講起,由於實際狀況中我也會直接這麼作,可是這麼作帶來的後果是以下:iview

在須要隱藏的頁面來上那麼一句 _tabBarController.tabBar.hidden = YES;ide

tabBar的確隱藏了,可是tabBar的位置會出現一部分空缺,以下圖ui

tabBar空缺
爲何會出現上面的狀況,從

立體結構圖

view層級圖
從上面的這幅圖能夠看出,tabBar的確隱藏了,可是咱們看到的空缺部分是創建在UINavigationController抑或是UITabBarController的某一類view上的一部分。(推測)而這個view又對controller.view進行了約束,致使了不管咱們如何進行

[self.view addSubview:subView];this

抑或是改變_view的大小,這部分的空缺仍然沒法填補。spa

不隱藏以前的樣子 code

不隱藏時候的樣子
如何去填補這部分黑影? 我嘗試了一下從新佈置_view的frame讓其覆蓋,可是老是不能完成覆蓋。 所以,我我的能力有限,沒法解決這樣的問題。何況在實際應用場合當中,我也不會經過這一方式來使其隱藏,畢竟界面的部分空缺對用戶體驗有必定減分。

沒有提供實際有效的方法,反而帶來了個坑。所以這部份內容也只能做爲‘讓讀者少走彎路’的內容看看哈哈哈哈。cdn

2.重寫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 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;

3.self.hidesBottomBarWhenPushed = YES;

這類方法和上述的第二種方法修改的對象也是一致的。 可是修改的時候須要注意以下幾個問題

  • 1.問題一:secondVC.hidesBottomBarWhenPushed = YES;

此時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找到了網友解答(以下):

網友解答

  • 2.問題二:self.hidesBottomBarWhenPushed = YES;

若是在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內部的寬距進行設置。


參考

蘋果文檔-hidesBottomBarWhenPushed

stackoverflow-1

stackoverflow-2

相關文章
相關標籤/搜索