UITabbarController
+ UINavigationController
的組合很經常使用。通常是UITabbarController
作為window
的根檢視控制器,然後其他VC使用UINavigationController
因此,在其他VC進行push操作的時候,常常需要將tabbar隱藏起來,在pop回去的時候重現顯示。這個需求大多通過UIViewController
的hidesBottomBarWhenPushed
的屬性來設定。設定為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);
}
*/
複製程式碼