Search code examples
iosswiftuinavigationcontrolleruitabbarcontroller

How to hide UITabBar when going back in UINavigationController


I have three viewControllers:

  • FeedController (UITabBar is visible)
  • PostController (UITabBar is hidden)
  • UserController (UITabBar is visible

I do this with the following code, from FeedController to PostController:

let postVC = PostController()
postVC.hidesBottomBarWhenPushed = true
pushViewController(postVC, animated: true)
postVC.hidesBottomBarWhenPushed = false

Then, from PostVC to UserVC:

let userVC = UserController()
userVC.hidesBottomBarWhenPushed = false
pushViewController(userVC, animated: true)

It works great. It shows the UITabBar everywhere except when navigating to a Post. However, the problem occurs when I go to a User Profile (UserController) from within a Post. It shows the UITabBar on the profile, as intended, but when I navigate back (using the back button in my UINavigationController) the UITabBar is still visible. I want it to be hidden again when I go back from the userVC to the postVC.

Is there any way I can accomplish this?


Solution

  • try in your post viewController:

      override func viewWillDisappear(_ animated: Bool) {
       postVC.hidesBottomBarWhenPushed = true
    }
    

    That will call it when the view is about to disappear but not when it appears so it should hide when you go back.