1

I created an iOS app with Xcode and Swift. My Storyboard contains five ViewControllers managed by a single TabBarController.

Is there a way to perform a segue from a UITabBarController to a UIViewController without the TabBar at the bottom disappears?

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Normally that would be done by making the segue from a contained controller (in a navigation controller?) rather than the tab bar controller. Any reason not to go that way? – Phillip Mills Jun 07 '16 at 12:39
  • It would be easier not to use NavigationController. –  Jun 07 '16 at 12:40

5 Answers5

1

The best way to implement this is to wrap your view controllers contained in the tab bar controller in a UINavigationController. Then inside of that view, you can push the new view onto the Navigation Controller and preserve the Tab Bar controller.

Check out this link about combining view controllers https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html

hawkstrider
  • 4,141
  • 16
  • 27
  • But that code don't work this way: func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { if(shortcutItem.type == "icons.quickaction.home"){ let sb = UIStoryboard(name: "Main", bundle: nil) let vc = sb.instantiateInitialViewController() window?.rootViewController = vc window?.rootViewController?.performSegueWithIdentifier("mysegue", sender: nil) } } –  Jun 07 '16 at 12:42
  • Do you know how to change the code above to use it with `TabBarController->NavigationController->ViewController` ? –  Jun 07 '16 at 12:44
  • You will have to rethink your view hierarchy. If you have a segue setup between the tab bar controller and another view, it will not preserve the tab bar controller. – hawkstrider Jun 07 '16 at 12:44
  • great answer - simple and leveraging the storyboard for the heavy lifting. thanks! – nbpeth Sep 03 '17 at 20:54
1
UIWindow root
|
UITabbarController
|
|- UINavigationController - navigationStack
|                                         |- 1 - SomeViewController
|                                         |- ...
|                                         |- ...
|                                         |- N'th...SomeViewController
|- SomeViewController
|- SomeViewController
|- SomeViewController

This above is the correct view controller hierarchy. Trying to do it any other way doesn't make any sense. In the example I only embedded the first set of controllers inside a navigation controller. It will do what you asked for. You can use this embedding in any of the tab bar positions.

The TabBarController uses VC containment to show controllers. You can embed your controllers like this

enter image description here

Earl Grey
  • 7,426
  • 6
  • 39
  • 59
  • Okay, I just did it like this. But why does my app always crash now when using QuickActions with this code: `func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { if(shortcutItem.type == "quickaction.home"){ let sb = UIStoryboard(name: "Main", bundle: nil) let vc = sb.instantiateInitialViewController() window?.rootViewController = vc window?.rootViewController?.performSegueWithIdentifier("newnew", sender: nil) } }` –  Jun 07 '16 at 13:13
  • Why are you setting the root view controller again inside the method? If you have a storyboard based app the this is already done via configuration. – Earl Grey Jun 07 '16 at 14:53
  • I made this by mistake. –  Jun 07 '16 at 16:17
1

Solution:

I was asking this question to handle QuickActions this way. Here's the working solution, without the TabBar disappears: https://stackoverflow.com/a/37710017/6423177

Community
  • 1
  • 1
0

If the view controller you are segueing to is among the tab bar view controllers then all you need is:

self.selectedIndex = INDEX_OF_VIEWCONTROLLER_IN_TABS

That will move to the controller as though you tapped on a tab.

So, you can do let tabVC = storyboard.instantiateViewController("TABIDENTIFIER") as! UITabBarController (or CustomTabBarController)

then select the index tabVC.selectedIndex = index

Cjay
  • 1,093
  • 6
  • 11
  • Yes, the ViewController I'm segueing to is among the TabBarController: `TabBarController->ViewController`. –  Jun 07 '16 at 13:04
  • Then: 1. Instantiate the TabBarController from storyboard. 2. set the selectedIndex When it displays it will show the view controller at that index – Cjay Jun 07 '16 at 13:07
0

If you're performing segue from UITabBarController to the UIViewController, you cannot keep the TabBar showing, although if you perform segue from on of the view controller (i.e. a tab) to the UIViewController the TabBar will not be hidden.

Ravi Sisodia
  • 776
  • 1
  • 5
  • 20