iTranslated by AI
[Tips] Easily Displaying UIViewController from SwiftUI
I will share a small technique for using UIViewController from SwiftUI.
It's a simple technique, but since I haven't seen it in many articles, I'm documenting it here.
Common Example of Calling UIViewController from SwiftUI
When you want to use a UIViewController from SwiftUI, you use UIViewControllerRepresentable.
struct ViewControllerWrapper: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ViewController {
ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
// Update process
}
}
While this is a perfectly valid approach, it is a bit tedious to declare a new UIViewControllerRepresentable type every time for each UIViewController type.
Personally, I also find it a bit troublesome to come up with names for them.
Making it Generic Using Generics
We can prepare a single generic type using generics.
Below is an example implementation.
The amount of code is somewhat long, but it simply injects closures into each function of UIViewControllerRepresentable from the outside.
The key point is being able to specify the concrete types for UIViewController and Coordinator externally.
public struct UIViewControllerRepresenter<ViewController: UIViewController, Coordinator>: UIViewControllerRepresentable {
private let makeCoordinatorHandler: @MainActor () -> Coordinator
private let makeUIViewControllerHandler: @MainActor (Context) -> ViewController
private let updateUIViewControllerHandler: @MainActor (ViewController, Context) -> Void
private let sizeThatFitsHandler: @MainActor (ProposedViewSize, ViewController, Context) -> CGSize?
public init(
makeCoordinator: @escaping @MainActor () -> Coordinator = { () },
makeUIViewController: @escaping @MainActor (Context) -> ViewController,
updateUIViewController: @escaping @MainActor (ViewController, Context) -> Void = { _, _ in },
sizeThatFits: @escaping @MainActor (ProposedViewSize, ViewController, Context) -> CGSize? = { _, _, _ in nil }
) {
self.makeCoordinatorHandler = makeCoordinator
self.makeUIViewControllerHandler = makeUIViewController
self.updateUIViewControllerHandler = updateUIViewController
self.sizeThatFitsHandler = sizeThatFits
}
public func makeCoordinator() -> Coordinator {
makeCoordinatorHandler()
}
public func makeUIViewController(context: Context) -> ViewController {
makeUIViewControllerHandler(context)
}
public func updateUIViewController(_ viewController: ViewController, context: Context) {
updateUIViewControllerHandler(viewController, context)
}
@available(iOS 16.0, tvOS 16.0, *)
@MainActor
public func sizeThatFits(_ proposal: ProposedViewSize, uiViewController: ViewController, context: Context) -> CGSize? {
sizeThatFitsHandler(proposal, uiViewController, context)
}
}
Usage Example
Once you have prepared a type like this, you can just call it whenever you want to display a UIViewController.
There is no need to declare a new type.
struct FooView: View {
var body: some View {
UIViewControllerRepresenter { _ in
FooViewController()
}
}
}
If you need a Coordinator or need to perform some processing during updates, you can also inject those with closures.
struct BarView: View {
var body: some View {
UIViewControllerRepresenter {
BarCoordinator()
} makeUIViewController: { context in
let viewController = BarViewController()
viewController.delegate = context.coordinator
return viewController
} updateUIViewController: { _, _ in
print("updated")
}
}
}
class BarCoordinator: BarDelegate {
// omitted
}
Note
Keep in mind that this is strictly a way to omit type declarations and does not offer complete flexibility.
For instance, since dismantleUIViewController in UIViewControllerRepresentable is a static function, it cannot be injected from the outside, so it is not covered here.
Conclusion
I have shared a technique for using UIViewController from SwiftUI.
Although it lacks complete flexibility, it is a very convenient method that can be reused in many situations once added to a project.
I hope this article helps you with your development.
Discussion