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:
- Passing the
view
andstoryboard
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. - 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!