How to properly set up UITabBarController programmatically with separate storyboards

567 views Asked by At

I have three storyboards in my project. There is a main and two separate workflow storyboards. Each storyboard is embedded in its own navigation controller.

Since I have broken up the storyboards into workflows I have to programmatically add each workflow to the tab bar. This is working correctly.

My issue occurs when I try and push a view (within a workflow) onto the workflows navigation controller. It seems that the navigation controller for a workflow is never being used. I verified this by changing the navigation bar color for each workflow.

I have tried two options, each set up on a workflow.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let mainStoryboard = UIStoryboard(name: "Workflow 1", bundle: nil)
    let actionItemStoryboard = UIStoryboard(name: "Workflow 2", bundle: nil)

    let workflow1Controller = mainStoryboard.instantiateViewController(withIdentifier: "navigationController1")
    let workflow1TabItem = UITabBarItem(title: "Item 1", image: nil, selectedImage: nil)
    workflow1Controller.tabBarItem = workflow1TabItem

    let workflow2Controller = actionItemStoryboard.instantiateViewController(withIdentifier: "workflow2")
    let workflow2TabItem = UITabBarItem(title: "Item 2", image: nil, selectedImage: nil)
    workflow2Controller.tabBarItem = workflow2TabItem

    self.viewControllers = [workflow1Controller, workflow2Controller]
}

Option 1

Setting the tab view controller to point to the navigation controller (pretty sure this is incorrect but was a test I did). This displays the views how I want but doesn't show the navigation item (back button).

Error pushing navigation controller


Option 2

Setting the tab view controller to point to the list view (see screen shots below). This displays the back button but upon clicking on the cell the last view is displayed "over" the tabs.

Error pushing list view


Main Storyboard enter image description here

Workflow 1 Storyboard Workflow 1 Storyboard

Workflow 2 Storyboard Workflow 2 Storyboard

1

There are 1 answers

0
sargturner On BEST ANSWER

In order to solve this I ended up doing the following:

  • Remove the navigation controllers from the workflows
  • Create a "MockTabController" (just a UIViewController with a UITabBar placed on the bottom of the view)

Now that I have a UIViewController with a UITabBar I can have each workflow view extend this view instead of a UIViewController and thus a tab bar is consistent throughout my app (where I want it).

For instances where the workflow has a UITableViewController I simply embed the UITableViewController inside a ViewController as a child view. The ViewController then extends my MockTabController and the result is a TableViewController with a tab bar that doesn't need any modifications to work.

In order to simplify the navigating throughout the app, I simply reset the navigation stack back to the beginning of the tab controller. Clicking a tab bar item unwinds all the workflow and then pushes the start of a new workflow.