[英]How can I observe the event that UITabBar is hidden or not?
我在push的時候用的是hidesBottomBarWhenPushed
方法,很多地方UITabBar在push的時候需要隱藏,pop的時候不隱藏,那么如何觀察事件呢?
實際上還有另一種方法可以觀察 UITabBar 是否隱藏,它是使用鍵值觀察 (KVO)。 我有一個類似的問題,並用它來查找tabBar
顯示或隱藏。
它看起來像這樣。
class TabBarController: UITabBarController {
// custom code
var observation: NSKeyValueObservation?
convenience init() {
self.init(nibName: nil, bundle: nil)
observation = observe(
\.tabBar.isHidden,
options: [.old, .new]
) { object, change in
print("TabBar isHidden changed from : \(change.oldValue), updated to: \(change.newValue)")
}
}
deinit {
observation = nil
}
}
如果在生產中使用它,請確保移除觀察者並遵循 KVO 的最佳實踐。 希望能幫助到你。
沒有委托或通知讓您知道 tabBar 是否可見。 您可以檢查 tabBar 的 window 屬性
if tabBarController!.tabBar.window == nil {
}
或隱藏 BottomBarWhenPushed
if hidesBottomBarWhenPushed {
}
變通。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.