从SwiftUI用带有后退按钮的UINavigation推送UIViewController?

问题描述

我需要使用带有后退按钮的UINavigation从SwiftUI打开UIViewController。

目前,我已经通过ViewControllerRepresentable和NavigationLink实现了它的工作,但是我有两个Navigation SwiftUI(带有后退按钮的NavigationLink)和没有后退按钮的认UINavigation,并且我想执行认的UINavigation并在后面按钮并隐藏SwiftUI(NavigationLink)导航。

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(){}

谢谢。

解决方法

如果您要使用NavigationView/NavigationLink,则无需使用UINavigationController,只需在UIViewController上使用可表示形式,例如

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
   }
}