Swift UI - Sizuha/devdog GitHub Wiki

UIKitと連携

UIKitのViewからSwiftUIのViewに遷移する

let view = UIHostingController(rootView: ContentView())
view.modalPresentationStyle = .fullScreen
self.present(view, animated: true)

SwiftUIのViewからUIKitのViewに遷移する

struct ViewControllerRepresentable: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> ViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "vc") as! ViewController
        vc.modalPresentationStyle = .overFullScreen
        return vc
    }

    func updateUIViewController(_ uiViewController: ViewController, context: Context) {
    }
}
@State private var isPresented: Bool = false
    
var body: some View {
    Button(action: {
        isPresented = true
    }) {
        Text("Return")
    }
    .fullScreenCover(isPresented: $isPresented) {
        ViewControllerRepresentable()
    }
}