UITabbarController + UINavigationController隱藏tabbar

kim_jin發表於2017-12-13

UITabbarController + UINavigationController 的組合很經常使用。通常是UITabbarController作為window的根檢視控制器,然後其他VC使用UINavigationController

因此,在其他VC進行push操作的時候,常常需要將tabbar隱藏起來,在pop回去的時候重現顯示。這個需求大多通過UIViewControllerhidesBottomBarWhenPushed的屬性來設定。設定為false的時候,跳轉到下一個vc時就會將tabbar隱藏起來,但是在UINavigationController的層次結構中,你將會發現,pop回來的時候,tabbar也沒有出現。

在SO上找到下面這個答案

hidesBottomBarWhenPushed = NO not working?

UINavigationController中,如果設定了其中一個VC的話,UINavigationController也會將層次結構中其他VC也一併設定上,所以哪怕將hidesBottomBarWhenPushed設定為false也沒有效果

不過該答案下面提供的解決程式碼貌似使用不了,我自己改了一下他的程式碼:

經指正,hidesBottomBarWhenPushed是放在要push的vc上,而不是放在當前的vc。沒注意這個屬性中的When.

  • swift
/*
override var hidesBottomBarWhenPushed: Bool {
        get {
            return navigationController?.topViewController != self
        }
        set {
            super.hidesBottomBarWhenPushed = newValue
        }
    }
*/
複製程式碼
  • oc
/*
- (BOOL) hidesBottomBarWhenPushed {
    return (self.navigationController.topViewController != self);
}
*/
複製程式碼

相關文章