Search code examples
iosswiftuitableviewuitabbarcontrollersegue

Tab Bar disappears on segue


Storyboard

What I want to achieve:

  • I have a "Login" ViewController which segues to a Tab Bar Controller.
  • My Tab Bar Controller goes to a Navigation Controller and then "Home" ViewController with a Table View. This view is my "Home/Welcome" page. I want there to be a Tab Bar at the bottom as well as a static table which I can edit with Prototype Cells. Each cell goes to a different ViewController.
  • Then, I want to be able to tap each Cell (I have "Info" setup for now) and it segue to the corresponding ViewController. For now, the "Info" cell should go to the second Navigation Controller and then "Information" ViewController.
  • From there, I want another static table which I can edit with Prototype Cells. Each cell will go to a seperate stack of ViewControllers.

My problem: When I segue from the "Info" cell in the "Home" ViewController, the Information tab bar "pushes" up, and the Tab Bar disappears. I've experimented with other segue forms but none of them maintain the Tab Bar. I want it so you can use either the Tab Bar to navigate to the Info ViewController or the cell on the Home/Welcome ViewController to navigate to the Info page.

I have no code at all at this point, I am just using these segues. Does anyone have any idea how I can achieve what I want to achieve, and if so, provide as much sample code as I'm extremely new to Swift.

Any help is appreciated! Thanks :)


Solution

  • Don't add segue to open the "Information" ViewController which is part of UITabBarController.

    You must have used didSelectRowAt for cell selection in tableview. Just add one line in it as it is :

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true) {
        self.tabBarController?.selectedIndex = 1
    }
    

    This will change the tab and open the "Information" ViewController.

    If you want to load the "Information" ViewController with data relevant to cell selection you can go with Protocols or NotificationCentre which can pass data to "Information" ViewController from Homeviewcontroller on cell selection.