Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I observe the event that UITabBar is hidden or not?

I use the method hidesBottomBarWhenPushed when push, and at many places, the UITabBar needs hidden when push and not hidden when pop back, so How can I observe the event ?

like image 671
QuakOrigin Avatar asked Oct 16 '25 05:10

QuakOrigin


1 Answers

There is actually another way to observer if when a UITabBar is hidden or not, it's by using Key-Value Observing (KVO). I had a similar problem and used it to find when the tabBar got displayed or hidden.

It can look like something like this.

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
  }
}

If use this in production, make sure you remove the observer and follow best practice for KVO. Hope it helps.

like image 143
Ben Avatar answered Oct 18 '25 21:10

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!