How to Navigating from SwiftUI View to UIKit UIViewController
Asked Answered
F

6

12

As of now, I have an application built entirely using UIKit. However, I wish to be able to start implementing some SwiftUI Views to replace some UIViewControllers.

I've been able to do this to navigate from the UIViewController to SwiftUI View on button tap:

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

My question is, how would I transition from a single SwiftUI View to a UIViewController?(Since the rest of the application is in UIKit)? I've got a button in the SwiftUI View, to navigate back to the UIViewController on tap. I've tried:

  1. Passing the view and storyboard objects to the SwiftUI View, then calling doing something similar to the code above to change the current view controller. However, when tried on the simulator nothing happens.
  2. Using .present to show the SwiftUI View modally. This works and I can allow the SwiftUI View to .dismiss itself. However, this only works modally, and I hope to make this work properly (i.e change screen)

Here is my simple SwiftUI View:

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.")
        }
    }
}

I'm having trouble understanding how to properly integrate SwiftUI into UIKit NOT the other way around, and I'm not sure if UIViewControllerRepresentable is the answer to this. Any solution to this, alternatives or helpful knowledge is very much appreciated. Thanks again!

Flagstad answered 19/2, 2020 at 18:3 Comment(2)
Hi Chiah, I am stuck in a similar situation. Were you able to find a solution to this?Acquaintance
Hey! Unfortunately i was not able to overcome this problem. Since my application is relatively small, I've instead just re-wrote everything in SwiftUI (and to be honest it's pretty good!). Sorry though!Flagstad
S
11

Ciao,

I tried to follow your approach by using closure callbacks.

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

When you present your UIHostingController, you want to implement the 2 closure callbacks:

@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)
}
Siward answered 19/2, 2020 at 18:27 Comment(1)
Similarly, I mentioned that tried this out and it works when presenting the SwiftUI view modally, but doesn't work if I want to properly transition(i.e not modally, covers the whole screen and cannot be swiped down)!Flagstad
M
4

Wrap your UIViewController to UIViewControllerRepresentable wrapper.

struct LessonDetailsViewControllerWrapper: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController

    func makeUIViewController(context: UIViewControllerRepresentableContext<LessonDetailsViewControllerWrapper>) -> UIViewController {
        let viewController = LessonDetailsViewController()
        return viewController
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<LessonDetailsViewControllerWrapper>) {}
}

Then from your SwiftUI view Navigate using Navigation Link:

  NavigationLink(destination: LessonDetailsViewControllerWrapper()) {
                            LessonRow(lesson: lesson)
                        }
Mailand answered 31/1, 2023 at 7:50 Comment(0)
H
1

You can do the same, only in mirror order, like following (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)
        }
    }) {
Hairdresser answered 19/2, 2020 at 18:24 Comment(1)
Hi, as I mentioned, I tried this (and just tried again), but nothing happens when the button is clicked!Flagstad
P
1

you can change the presentation style and transition style to present the controller full screen

let vc = UIHostingController(rootView: LoginView())
vc.modalPresentationStyle = .fullScreen
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true)
Pontifex answered 25/10, 2021 at 10:25 Comment(0)
A
0

Here is the example of push navigation controller from UIKit Button Press

let controller = UIHostingController(rootView: LoginViewController())
    self.navigationController?.pushViewController(controller, animated: true)
Autostrada answered 2/2, 2022 at 11:17 Comment(0)
P
0

Here is the example, you can navigate SwiftUi screen to Uikit View controller screen. Pass destination as LoginVCControllerRepresentable on NavigationLink click.

import Foundation
import SwiftUI
struct LoginVCControllerRepresentable: UIViewControllerRepresentable {

typealias UIViewControllerType = UIViewController

func makeUIViewController(context: UIViewControllerRepresentableContext<LoginVCControllerRepresentable>) -> UIViewController {
   
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as! LogInViewController
      return viewController
  }





func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<LoginVCControllerRepresentable>) {}
}
Protuberance answered 22/7, 2024 at 5:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.