0
votes

My storyboard is arranged as so.

Red: Tab Bar Controller that segues to...
Orange: Nav Controllers that have embedded...
Green: View Controllers

storyboard arrangement

I want to make my Middle Tab View (green) present itself modally, sort of like how the reddit app does it with its middle 'Post to Reddit' button. When this Middle View is dismissed the original Tab that was open beforehand will be returned to. How can this be done?

1
Create a show segue from the tab bar controller to the green view controller. When you want to present the green view controller morally, trigger that segue.Paulw11
@Paulw11 how do I trigger the segue after connecting the tab bar controller with the view controller?Matthew
@Matthew - your layout shows your "green view controller" as the root VC of a navigation controller... what do you really want to do? Does your green VC push to another VC (and maybe more), and that's why you have it in a nav controller? And you say "When this Middle View is dismissed the original Tab ..." so, you don't really want the middle view to be one of the tabs... you want it shown as a modal "popup"?DonMag
@DonMag yes I want the middle view to be a modal "popup" type view but I want to be able to access it from the Tab Bar. The middle view is embedded in a nav controller because I am going to make it have multiple "Next" screens. To summarise, middle tab bar item opens up green view in popup style window with (eventually) multiple Next screens.Matthew

1 Answers

0
votes

One way to do this...

Give the UINavigationController that is your 2nd tab a StoryboardID - such as "createItemsNavController" - then implement shouldSelect in your custom tab bar controller class.

If the 2nd tab is selected (tabs, like all arrays, are Zero based), instantiate your "createItemsNavController" and present it, returning false for shouldSelect:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

    guard let indexOfTab = viewControllers?.firstIndex(of: viewController) else {
        return true
    }
    
    if indexOfTab == 1 {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "createItemsNavController") as? UINavigationController {
            present(vc, animated: true, completion: nil)
        }
        return false
    }
    
    return true

}

If you're going that route, you could also (and it might be a good idea to) replace that Tab connection in your Storyboard with a blank view controller... as you probably want to avoid having it loaded by the tab bar controller, even if you never allow that tab to be activated.

As a side note: that could be a very confusing UX. Users (and Apple) like apps that conform to common interface actions. Since users are familiar with tab bars, where selecting a tab button switches to that tab, changing the functionality in this way may be frowned upon.

Of course, it's your app, and your design choice...