Search code examples
iosswiftuitabbarcontrollersegue

TabBarController disappears when I segue back to the view from another view


I have a viewController that is embedded in a nav controller. This nav controller is then embedded in a tab bar controller.

I have another viewController that is not supposed to be accessible from the tabBarController. It should only be accessible from the first viewController using a button. From the secondViewController, I made a UIBarButtonItem to move back to the original view. From the first view to the second view and vice versa, I used a Storyboard reference to move to and from from the views.

However, when I move from the first view to the second view, the tab bar controller disappears (like it should). When I move back to the first view, the tab bar controller disappears and I cannot move between tabs anymore.

I tried including:

self.hidesBottomBarWhenPushed = false

on the first view and

self.hidesBottomBarWhenPushed = true

on the second view

and nothing seems to work. The tab bar controller disappears every time i move from the second view to the first view.


Solution

  • You are following a wrong hierarchy. You are actually using seagues to go back and forth. This creates a new instance every time you try to come back to the first controller.

    Let's make it clear:

    You need to follow the below approach:

    1 You have two controllers A and B.

    2 Use self.hidesBottomBarWhenPushed = true in viewDidLoad or viewWillAppear of controller A.

    3 Controller A is embedded in a navigation controller which is further embedded in a UITabBarController.

    Tapping a button in controller A, you need to push to controller B. So you can use segue for this or you can do it programatically like:

    let controllerB = B()
    A.navigationController?.pushViewController(controllerB, animated: true)
    

    4 Go Back to Controller A on the tap UIBarButtonItem. So your code in the action of UIBarButtonItem should be something like:

    self.navigationController?.popViewController(animated: true)
    

    Remember you should not should segue to go back to the previous controller.