I'm currently working on a project using both swiftUI and UIKit. I made a ViewController to work on, but now I want to use that as a navigation link in the original content view.
The first thing I tried was the normal
NavigationLink{
ViewController()
} label: {
Text("XXX")
}
but I got these two errors:
Generic struct 'NavigationLink' requires that 'ViewController' conform to 'View'
Static method 'buildBlock' requires that 'ViewController' conform to 'View'
I found this this post which said to use a struct, but that didn't work either and I got other errors too.
So how can I efficiently use my viewcontroller as a navigationlink
You do need to wrap your view controller in a SwiftUI struct that conforms to the
UIViewControllerRepresentable
protocol. This allows you to use a UIKit view controller in your SwiftUI view hierarchy.There's a very similar protocol,
UIViewRepresentable
, that works for UIKit views that aren't controllers - it works in almost exactly the same way.So if your UIKit view controller is called
MyViewController
, we could wrap it in a Swift view that we'll callMyView
. There are two methods that we have to implement:That is all you have to do to get a version of your controller to work within SwiftUI's view hierarchy. You have to remember to use the SwiftUI wrapper rather than your UIKit view controller directly, e.g.:
There's more to do if you need to pass information in to the view controller to set it up, to get it to respond to changes in your SwiftUI state, or if you want to update state based on events or actions in the controller. But that's outside the scope of your question, I think.