Search code examples
iosswiftuitabbarcontrollersegueuitabbar

TabBar is hidden after going back to the initial ViewController of a TabBarController Using Segue


I have a tab bar controller with three table view controllers and the second VC is embedded in a navigation Controller. in the second VC, I made the tabBar hidden using this line self.tabBarController?.tabBar.isHidden = true and I created a bar button to go back to the first view controller which is the "home" VC using segue with modal presentation.

Screenshot of my StoryBoard

My problem is after hitting the back button and going back to home VC from the second VC, the tabBar is still hidden even though I put self.tabBarController?.tabBar.isHidden = false in the home VC's viewWillAppear method and the second VC's viewWillDisappear method.

Here is the result that I expected vs what I got

expected home VC

result home VC

How can I make the Tab Bar show?


Solution

  • When you are using the modal presentation segue, you are creating a completely new instance of HomeViewController. The new HomeViewController is not linked to the TabBarController in your hierarchy.

    Here's you initial view hierarchy:

    TabBarController
     -> HomeVC
     -> CreateVC (Navigation Controller)
        -> CreateQuizVC
     -> SavedVC
    

    Now after tapping the back button you'll get the following:

    TabBarController
     -> HomeVC
     -> CreateVC (Navigation Controller)
       -> CreateQuizVC
         -> HomeVC(2)
     -> SavedVC
    

    What you could do is, instead of using the segue to go back, add an IBAction in your code to set the selectedIndex of the TabBar programatically, and link the Back UIBarButtonItem to this IBAction.

    @IBAction func backButtonAction(_ backButton: UIBarButtonItem) {
        // Keep in mind that the CreateQuizVC is embeded in a NavigationController.
        // The NavigationController is the child of the TabBarController
        navigationController?.tabBarController?.selectedIndex = 0
        navigationController?.tabBarController?.tabBar.isHidden = false
    }
    

    However, my suggestion is you use the TabBar as it's intended by Apple. Don't hide it while you're presenting your CreateQuizVC, and use the TabBar to navigate between the tabs. This will help with user experience, since everybody on iOS is expecting this behaviour from a TabBar.