I am working to implement some SwiftUI content into my existing app. I currently have a UIViewController, which hosts a MTKView for camera preview.
I have created a new SwiftUI view, which is now my root view, as set in my SceneDelegate.swift
file. The SwiftUI view loads at launch, as expected. Now, I would like to create a segue in which, when a user taps on a row in my List, it will segue, full-screen to my existing UIViewController. Here is how I'm calling that;
var body: some View {
VStack {
NavigationView {
List(sessionTypes) { session in
NavigationLink(destination: CameraControllerWrapper()) {
SessionRow(session: session)
.frame(height: 40.0)
}
}
.navigationBarTitle(Text("Camera Types"))
}
}
}
For posterity, here is my CameraControllerWrapper
UIViewControllerRepresentable;
struct CameraControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = CameraController
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraControllerWrapper>) -> CameraControllerWrapper.UIViewControllerType {
return CameraController()
}
func updateUIViewController(_ uiViewController: CameraControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<CameraControllerWrapper>) {
//
}
}
While this "works," my app crashes as soon as the CameraController is called, as it seems any of my IBOutlets cannot be found. CameraController is a UIViewController built in the storyboard.
To go back from the SwiftUI screen, we need to add a UINavigationController and pass our root navigation controller to the SwiftUI screen. We pushed to the SwiftUI screen before using the UIHostingController . Now, we will use the UINavigationController to pop back to the UIKit screen.
To achieve this, you would need to create a new struct that conforms to UIViewControllerRepresentable . This acts like a wrapper for UIKit s UIViewController . There is a similar protocol for UIView , UIViewRepresentable . Alternatively, this struct will work if you have your ViewController inside a storyboard.
SwiftUI works seamlessly with the existing UI frameworks on all Apple platforms. For example, you can place UIKit views and view controllers inside SwiftUI views, and vice versa.
The updateUIViewController method is called when there is an update from SwiftUI. This is a place for us to update our view controller. Again, for our simple view controller, we don't need this. Actually, Swift can infer UIViewControllerType from makeUIViewController and updateUIViewController methods.
I managed to resolve this by realizing that I needed to instantiate the UIViewController from the Storyboard, and not in code (as I had built its layout in Storyboard, alongside some programatic elements). To use the above NavigationLink
, I needed to adjust my UIViewControllerRepresentable
as such;
struct CameraControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = CameraController
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraControllerWrapper>) -> CameraControllerWrapper.UIViewControllerType {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: CameraController = mainStoryboard.instantiateViewController(withIdentifier: "CameraController") as! CameraController
return mainViewController
}
func updateUIViewController(_ uiViewController: CameraControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<CameraControllerWrapper>) {
//
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With