Push UIViewController from SwiftUI with UINavigation with Back Button?

749 views Asked by At

I need to open UIViewController from SwiftUI with UINavigation having back button.

Currently, I had implemented this via ViewControllerRepresentable and NavigationLink, its works, but I have two Navigations SwiftUI(NavigationLink with back button) and default UINavigation without back button, and I would like to perform to have my default UINavigation with the back button and hide SwiftUI(NavigationLink) navigation.

struct UIKitVc : UIViewControllerRepresentable {


   let navigationController =  UINavigationController()

   func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
   }


   func makeUIViewController(context: Context) -> UINavigationController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "SearchResult") as! SearchResultViewController
      self.navigationController.addChild(viewController)

        return self.navigationController
   }
}


NavigationLink(destination: UIKitVc(){}

Thank you in advance.

1

There are 1 answers

2
Asperi On

If you're going to use NavigationView/NavigationLink then you don't need to use UINavigationController, just use representable over your UIViewController, like

struct UIKitVc : UIViewControllerRepresentable {


   func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
   }

   func makeUIViewController(context: Context) -> UIViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: 
            "SearchResult") as! SearchResultViewController

        // ... make additional set up here if needed

        return viewController
   }
}