0

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)
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50
  • If you work with `UIHostingController` in UIKit environment it has nothing to SwiftUI content... it just a view controller. – Asperi Jun 13 '20 at 17:20
  • Alright, I removed the SwiftUI tag for the second time. I did not add it the first time. – C. Skjerdal Jun 13 '20 at 17:39
  • What is the relation between `self.tabBarController` and `UIHostingView`? Please add more relevant details. – koen Jun 15 '20 at 12:55
  • There is no UIHostingView, the UIHostingController is just another tab on the TabBarController – C. Skjerdal Jun 15 '20 at 14:14
  • 1
    Maybe this helps: https://stackoverflow.com/questions/15637357/how-to-switch-to-a-tabbar-item-in-the-more-view – koen Jun 16 '20 at 11:45
  • @koen that looks like the code that should work, I put it in and nothing changed. I'll dig some more at this issue because that code is what I assumed the code should be. – C. Skjerdal Jun 22 '20 at 15:14
  • I actually set the tabBarController to moreNavigationController.popViewController and it flickers between my current controller and the moreViewController – C. Skjerdal Jun 22 '20 at 15:35

2 Answers2

0

I don't know how you implemented the tabView but in SwiftUI the boilerplate code is like below:

struct ContentView: View {

@State private var selection = 0

var body: some View {
    TabView(selection: $selection){
        Text("First View")
            .font(.title)
            .tabItem {
                VStack {
                    Image("first")
                    Text("First")
                }
            }
            .tag(0)
        Text("Second View")
            .font(.title)
            .tabItem {
                VStack {
                    Image("second")
                    Text("Second")
                }
            }
            .tag(1)
    }
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
    ContentView()
 }
}

Here you can see the selection is 0, if you change it to 1 it will go to second tab when you open the view. Hope this help

Arafin Russell
  • 1,487
  • 1
  • 18
  • 37
  • No I am still using Storyboard, and I have a UITabBarController implemented. My 8th tab goes to a UIHostingController. Which is a method for slowly migrating to SwiftUI. For this I am looking for a Swift answer, not a SwiftUI answer. Thanks though! – C. Skjerdal Jun 13 '20 at 16:43
  • 1
    Your question says "SwiftUI". So please update your question to avoid confusion. – koen Jun 13 '20 at 16:53
  • 1
    I guess leaving in SwiftUI is good because people familiar with SwiftUI will understand UIHostingController best. Made a small tweak to improve. – C. Skjerdal Jun 13 '20 at 17:16
0

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

 self.navigationController?.popToRootViewController(animated: true)
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50