Search code examples
swiftuitabbarcontrolleruihostingcontroller

Open More Navigation Tab in Swift


I have a UIHostingController which contains my SwiftUI View. I want to return to the More Tab from the UIHostingController.

I tried calling

dismiss(animated: true, completion: nil)

which doesn't work. I tried changing the tabbar's selection, but this of course never goes to the more tab.

self.tabBarController!.selectedIndex = 5

I presume there is an easy function to make it pop up over my view, I just can't find it.

Edit: To explain further, I have a storyboard with several ViewControllers. One is a UIHostingController. Perhaps that detail doesn't matter, I am trying to open the list of 'more' items from a ViewController with Swift. The UIHostingController though uses a custom navigation, so the default back buttons aren't relevant.

Update: The closest code I have found is:

 self.tabBarController?.selectedViewController = tabBarController?.moreNavigationController

This however did not appear to work, but by calling the code below. I was able to flicker show the moreViewController.

self.tabBarController?.selectedViewController = tabBarController?.moreNavigationController.popViewController(animated: true)

Solution

  • Finally discovered it. This is how you return to the more controller, which is just the root view.

     self.navigationController?.popToRootViewController(animated: true)