Mixing UIKit and SwiftUI

IOS Development 10 min min read Updated: Nov 16, 2026 Advanced
Mixing UIKit and SwiftUI
Advanced Topic 4 of 4

Why Mix?

  • You are moving an old UIKit app to SwiftUI one screen at a time
  • A feature exists only in UIKit, or the UIKit version is better, such as a camera picker or a web view
  • You want to reuse a nice SwiftUI component inside an old app

SwiftUI inside UIKit

swift import SwiftUI import UIKit struct ProfileView: View { let name: String var body: some View { Text("Hello, \(name)").font(.largeTitle) } } class OldViewController: UIViewController { @IBAction func openProfile() { let host = UIHostingController(rootView: ProfileView(name: "Asha")) navigationController?.pushViewController(host, animated: true) } // embed a SwiftUI view as a child inside part of a screen func embedSwiftUI() { let host = UIHostingController(rootView: ProfileView(name: "Ravi")) addChild(host) host.view.frame = CGRect(x: 0, y: 100, width: view.bounds.width, height: 120) view.addSubview(host.view) host.didMove(toParent: self) } }

UIKit view inside SwiftUI

swift import SwiftUI import WebKit struct WebView: UIViewRepresentable { let url: URL func makeUIView(context: Context) -> WKWebView { WKWebView() // create once } func updateUIView(_ webView: WKWebView, context: Context) { webView.load(URLRequest(url: url)) // called when SwiftUI state changes } } struct BrowserScreen: View { var body: some View { WebView(url: URL(string: "https://www.apple.com")!) .ignoresSafeArea() } }
Newer option: SwiftUI also has a built-in WebView in recent iOS versions. Use it when your minimum version allows, and fall back to WKWebView through a representable for older versions.

UIViewControllerRepresentable with a Coordinator

A Coordinator receives delegate callbacks from UIKit and sends them back to SwiftUI.

swift struct ImagePicker: UIViewControllerRepresentable { @Binding var image: UIImage? @Environment(\.dismiss) private var dismiss func makeUIViewController(context: Context) -> UIImagePickerController { let picker = UIImagePickerController() picker.delegate = context.coordinator picker.sourceType = .photoLibrary return picker } func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {} func makeCoordinator() -> Coordinator { Coordinator(self) } final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { let parent: ImagePicker init(_ parent: ImagePicker) { self.parent = parent } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { parent.image = info[.originalImage] as? UIImage parent.dismiss() } } }
Modern alternative: For picking photos, SwiftUI has PhotosPicker, shown in a later lesson. Use representables only when SwiftUI has no native option.

Conclusion

UIHostingController brings SwiftUI into UIKit and UIViewRepresentable brings UIKit into SwiftUI. You can migrate gradually.

Quick Summary: Hosting controllers embed SwiftUI in UIKit. Representables with Coordinators embed UIKit in SwiftUI.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators