Why this happens
Teams migrating from Objective-C or pure UIKit to SwiftUI rarely rewrite an entire app at once. The practical path is incremental: new screens in SwiftUI, legacy screens in UIKit, and a transition period where both coexist. Apple designed two official bridges for this: UIHostingController to host SwiftUI inside UIKit, and UIViewRepresentable to host UIKit inside SwiftUI. Understanding both directions lets you adopt SwiftUI at your own pace without a big-bang rewrite.
How to fix it
1. Embed a SwiftUI view in a UIKit view controller with UIHostingController
The simplest approach: create a UIHostingController whose rootView is your SwiftUI view, then add it as a child view controller.
import SwiftUI
import UIKit
final class ProfileHostingController: UIHostingController<ProfileView> {
init() {
super.init(rootView: ProfileView())
}
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// In your UIKit view controller:
let swiftUIVC = ProfileHostingController()
addChild(swiftUIVC)
swiftUIVC.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(swiftUIVC.view)
NSLayoutConstraint.activate([
swiftUIVC.view.topAnchor.constraint(equalTo: view.topAnchor),
swiftUIVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
swiftUIVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
swiftUIVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
swiftUIVC.didMove(toParent: self)
Container View (Storyboard) alternative — if you prefer a container view in a storyboard:
class ContainerViewController: UIViewController {
@IBOutlet private weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let child = UIHostingController(rootView: SwiftUIView())
addChild(child)
child.view.frame = containerView.bounds
child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
containerView.addSubview(child.view)
child.didMove(toParent: self)
}
}
2. Customize UIHostingController when needed
Override UIHostingController to adjust status-bar style, safe-area insets, or presentation behavior:
final class CustomHostingController<Content: View>: UIHostingController<Content> {
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
// React to safe-area changes if your SwiftUI view needs it
}
}
3. Use a UIKit view inside SwiftUI with UIViewRepresentable
When a SwiftUI screen needs a mature UIKit component (e.g., WKWebView, UITextView, a custom UIView subclass), conform to UIViewRepresentable:
import SwiftUI
import WebKit
struct WebView: UIViewRepresentable {
let url: URL
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.navigationDelegate = context.coordinator
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let request = URLRequest(url: url)
uiView.load(request)
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
final class Coordinator: NSObject, WKNavigationDelegate {
var parent: WebView
init(_ parent: WebView) { self.parent = parent }
// Implement delegate methods here
}
}
// Usage in SwiftUI:
// WebView(url: URL(string: "https://example.com")!)
For UIViewController subclasses, use UIViewControllerRepresentable instead — same pattern, different base protocol.
4. Handle deployment targets below iOS 13
SwiftUI is only available on iOS 13+. If your project’s deployment target is lower, guard every SwiftUI type and bridging code:
@available(iOS 13.0, *)
struct ModernSwiftUIView: View {
var body: some View { Text("Hello") }
}
@available(iOS 13.0, *)
final class ModernHostingController: UIHostingController<ModernSwiftUIView> {
init() { super.init(rootView: ModernSwiftUIView()) }
@MainActor required dynamic init?(coder: NSCoder) { fatalError() }
}
// In UIKit code:
if #available(iOS 13.0, *) {
let vc = ModernHostingController()
present(vc, animated: true)
} else {
// Fallback UIKit implementation
}
Without the @available guard, the compiler emits “‘SwiftUI’ is only available in iOS 13.0 or newer” errors.
How to prevent it
- Set a clear migration policy: decide which new screens are SwiftUI-only and which stay UIKit.
- Keep bridging code isolated: put all
UIHostingControllersubclasses andUIViewRepresentablewrappers in a dedicatedBridgingfolder. - Test on the minimum deployment target: run the app on a device/simulator at your lowest supported iOS version to catch missing
@availableguards. - Avoid deep view hierarchies mixing both worlds: prefer whole-screen handoffs (push a hosting controller, present a SwiftUI sheet) over embedding many tiny SwiftUI views inside UIKit cells.
- Use SwiftUI previews for bridged views: wrap the hosting controller or representable in a preview provider to iterate quickly.
FAQ
Can I push a SwiftUI view onto a UINavigationController stack?
Yes. Wrap the SwiftUI view in a UIHostingController and push that controller: navigationController?.pushViewController(UIHostingController(rootView: SwiftUIView()), animated: true). The hosting controller participates in the navigation stack like any UIViewController.
How do I pass data from UIKit to the hosted SwiftUI view?
Initialize the SwiftUI view with the data before creating the hosting controller: let hosting = UIHostingController(rootView: DetailView(item: selectedItem)). For updates, hold a reference to the hosting controller and replace its rootView or use a shared observable object (@StateObject / @EnvironmentObject) that both sides observe.
Does UIViewRepresentable work with AppKit on macOS?
No. On macOS the equivalent protocol is NSViewRepresentable (and NSViewControllerRepresentable). The method signatures are nearly identical (makeNSView, updateNSView), so you can share logic via #if os(iOS) / #if os(macOS) conditionals.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.