简体   繁体   中英

How to properly set up UITabBarController programmatically with separate storyboards

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).

推送导航控制器时出错


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.

推送列表视图时出错


Main Storyboard 在此处输入图片说明

Workflow 1 Storyboard 工作流程1故事板

Workflow 2 Storyboard 工作流程2故事板

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM