8

到目前为止,我有一个完全使用 UIKit 构建的应用程序。但是,我希望能够开始实现一些 SwiftUI 视图来替换一些 UIViewController。

我已经能够通过点击按钮从 UIViewController 导航到 SwiftUI 视图:

    @IBAction func buttonTapped(_ sender: Any) {
        let newView = UIHostingController(rootView: SwiftUIView(viewObj: self.view, sb: self.storyboard, dismiss: self.dismiss) )
        view.window?.rootViewController = newView
        view.window?.makeKeyAndVisible()
    }

我的问题是,我将如何从单个 SwiftUI 视图转换到 UIViewController?(因为应用程序的其余部分在 UIKit 中)?我在 SwiftUI 视图中有一个按钮,可以在点击时导航回 UIViewController。我试过了:

  1. view将和对象传递storyboard给 SwiftUI 视图,然后调用执行与上述代码类似的操作来更改当前视图控制器。但是,在模拟器上尝试时没有任何反应。
  2. 用于以.present模态方式显示 SwiftUI 视图。这行得通,我可以允许 SwiftUI 视图.dismiss本身。但是,这仅在模态下有效,我希望使其正常工作(即更改屏幕)

这是我的简单 SwiftUI 视图:

struct SwiftUIView: View {
    var viewObj:UIView? // Perhaps use this for transition back?
    var sb:UIStoryboard?
    var dismiss: (() -> Void)?

    var body: some View {

        Button(action: {
            // Do something here to Transition
        }) {
            Text("This is a SwiftUI view.")
        }
    }
}

我无法理解如何正确地将 SwiftUI 集成到 UIKit中,而不是相反,我不确定是否UIViewControllerRepresentable是这个问题的答案。非常感谢对此的任何解决方案、替代方案或有用的知识。再次感谢!

4

4 回答 4

6

再见,

我试图通过使用闭包回调来遵循您的方法。

struct SwiftUIView: View {
    var dismiss: (() -> Void)?
    var present: (()->Void)?

    var body: some View {
        VStack(spacing: 20) {
            Button(action: {
                self.dismiss?()
            }) {
                Text("Dismiss me")
            }
            Button(action: {
                self.present?()
            }) {
                Text("Present some UIViewController")
            }
        }
    }
}

当你展示你的 UIHostingController 时,你想要实现 2 个闭包回调:

@IBAction func buttonTapped(_ sender: Any) {
    let hostingController = UIHostingController(rootView: SwiftUIView())
    hostingController.rootView.dismiss = {
        hostingController.dismiss(animated: true, completion: nil)
    }
    hostingController.rootView.present = {
        let destination = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "VC_TO_PRESENT")
        hostingController.present(destination, animated: true, completion: nil)
    }

    present(hostingController, animated: true, completion: nil)
}
于 2020-02-19T18:27:10.537 回答
1

你可以做同样的事情,只是按照镜像顺序,比如跟随(scratchy)......

    Button(action: {
        if let vc = self.sb?.instantiateViewController(withIdentifier: "some_identifier") {
            self.viewObj?.window?.rootViewController = vc
            // or via present as alternate
            // self.viewObj?.window?.rootViewController.present(vc, animated: true, completion: nil)
        }
    }) {
于 2020-02-19T18:24:37.147 回答
0

这是来自 UIKit Button Press 的推送导航控制器的示例

let controller = UIHostingController(rootView: LoginViewController())
    self.navigationController?.pushViewController(controller, animated: true)
于 2022-02-02T11:17:45.790 回答
0

您可以更改演示样式和过渡样式以全屏呈现控制器

let vc = UIHostingController(rootView: LoginView())
vc.modalPresentationStyle = .fullScreen
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true)
于 2021-10-25T10:25:26.673 回答