title: SwiftUI: Pop to root view when selected tab is tapped again
body: <p>Starting point is a NavigationView within a TabView. I'm struggling with finding a SwiftUI solution to pop to the root view within the navigation stack when the selected tab is tapped again. In the pre-SwiftUI times, this was as simple as the following:</p>
  
  <pre><code>func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      let navController = viewController as! UINavigationController
      navController.popViewController(animated: true)
  }
  </code></pre>
  
  <p>Do you know how the same thing can be achieved in SwiftUI?</p>
  
  <p>Currently, I use the following workaround that relies on UIKit:</p>
  
  <pre><code>if let windowScene = scene as? UIWindowScene {
              let window = UIWindow(windowScene: windowScene)
  
              let navigationController = UINavigationController(rootViewController: UIHostingController(rootView:
                  MyCustomView() // -&gt; this is a normal SwiftUI file
                      .environment(\.managedObjectContext, context)
              ))
              navigationController.tabBarItem = UITabBarItem(title: "My View 1", image: nil, selectedImage: nil)
  
              // add more controllers that are part of tab bar controller
  
              let tabBarController = UITabBarController()
              tabBarController.viewControllers = [navigationController /*,  additional controllers */]
  
              window.rootViewController = tabBarController // UIHostingController(rootView: contentView)
              self.window = window
              window.makeKeyAndVisible()
          }
  </code></pre>