Why this happens
SwiftUI views are not UIView subclasses — they are value-type structs that SwiftUI renders through its own layout engine. UIKit, however, expects UIView instances in its view hierarchy. There is no direct UIView equivalent of macOS’s NSHostingView on iOS. Instead, Apple provides UIHostingController, a UIViewController subclass whose view property hosts the SwiftUI content. Because a view controller’s view is a UIView, you can embed it anywhere in a UIKit hierarchy using standard view controller containment.
The confusion often stems from trying to treat a SwiftUI view like a UIView subclass. The correct mental model is: a view controller is the bridge. You create a hosting controller for your SwiftUI view, then embed that controller as a child of your existing UIKit view controller. This gives the SwiftUI view a place in the responder chain, layout system, and lifecycle events.
How to fix it
1. Wrap the SwiftUI view in UIHostingController and add as child view controller
This is the standard, Apple-recommended approach. It works on iOS 13+ and gives the SwiftUI view full access to the environment, state updates, and safe-area insets.
import UIKit
import SwiftUI
final class ContainerViewController: UIViewController {
private var hostingController: UIHostingController<AnyView>?
override func viewDidLoad() {
super.viewDidLoad()
embedSwiftUIView()
}
private func embedSwiftUIView() {
// Your SwiftUI view — can be any View
let swiftUIView = Text("Hello from SwiftUI")
.font(.title)
.foregroundColor(.white)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
// 1. Create the hosting controller
let hosting = UIHostingController(rootView: swiftUIView)
hosting.view.translatesAutoresizingMaskIntoConstraints = false
// 2. Add as child view controller
addChild(hosting)
view.addSubview(hosting.view)
hosting.didMove(toParent: self)
// 3. Constrain the hosted view
NSLayoutConstraint.activate([
hosting.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hosting.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hosting.view.topAnchor.constraint(equalTo: view.topAnchor),
hosting.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
self.hostingController = hosting
}
}
Key steps:
1. UIHostingController(rootView:) — wraps the SwiftUI view.
2. addChild(_:) — begins the containment relationship.
3. view.addSubview(_:) — places the hosting controller’s view in the hierarchy.
4. didMove(toParent:) — completes the containment.
5. Set translatesAutoresizingMaskIntoConstraints = false and add Auto Layout constraints (or set frame manually).
2. Reuse a single hosting controller and swap its rootView
If you need to replace the SwiftUI content dynamically (e.g., navigation within a UIKit tab), keep one hosting controller and update its rootView property. This avoids repeatedly adding/removing child controllers.
final class DynamicContainerViewController: UIViewController {
private let hostingController = UIHostingController(rootView: EmptyView())
override func viewDidLoad() {
super.viewDidLoad()
setupHostingController()
showInitialView()
}
private func setupHostingController() {
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
addChild(hostingController)
view.addSubview(hostingController.view)
hostingController.didMove(toParent: self)
NSLayoutConstraint.activate([
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
func showInitialView() {
hostingController.rootView = AnyView(
VStack {
Text("First Screen")
Button("Switch") { showSecondView() }
}
)
}
func showSecondView() {
hostingController.rootView = AnyView(
VStack {
Text("Second Screen")
Button("Back") { showInitialView() }
}
)
}
}
Why this works: UIHostingController.rootView is a settable property. Changing it triggers a SwiftUI update cycle without tearing down the view controller containment.
3. Embed in a UIView subclass (not a view controller)
If you only have a UIView subclass (e.g., a custom UITableViewCell or a view loaded from a XIB), you cannot add a child view controller directly — only a UIViewController can be a parent. The workaround is to expose a method that accepts a parent view controller from the outside.
final class SwiftUIHostingView: UIView {
private var hostingController: UIHostingController<AnyView>?
/// Call this from the owning view controller.
func embed(in parent: UIViewController, rootView: some View) {
let hosting = UIHostingController(rootView: AnyView(rootView))
hosting.view.translatesAutoresizingMaskIntoConstraints = false
parent.addChild(hosting)
addSubview(hosting.view)
hosting.didMove(toParent: parent)
NSLayoutConstraint.activate([
hosting.view.leadingAnchor.constraint(equalTo: leadingAnchor),
hosting.view.trailingAnchor.constraint(equalTo: trailingAnchor),
hosting.view.topAnchor.constraint(equalTo: topAnchor),
hosting.view.bottomAnchor.constraint(equalTo: bottomAnchor)
])
self.hostingController = hosting
}
func updateRootView(_ rootView: some View) {
hostingController?.rootView = AnyView(rootView)
}
}
// Usage in a view controller:
final class ParentViewController: UIViewController {
private let hostingView = SwiftUIHostingView()
override func viewDidLoad() {
super.viewDidLoad()
hostingView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(hostingView)
NSLayoutConstraint.activate([
hostingView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
hostingView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
hostingView.widthAnchor.constraint(equalToConstant: 200),
hostingView.heightAnchor.constraint(equalToConstant: 200)
])
hostingView.embed(in: self, rootView: Text("Inside a UIView").padding().background(Color.green))
}
}
Important: The UIView itself never calls addChild(_:). The owning UIViewController does. This keeps the containment hierarchy valid.
How to prevent it
- Always use
UIHostingController— there is noUIViewsubclass that hosts SwiftUI on iOS. - Call
addChild→addSubview→didMove(toParent:)in that order; skipping any step breaks lifecycle events (viewWillAppear, rotation, safe-area updates). - Set
translatesAutoresizingMaskIntoConstraints = falsebefore adding constraints, or useframe-based layout if you prefer manual layout. - Keep a strong reference to the hosting controller (property) so it isn’t deallocated prematurely.
- When embedding in a
UIViewsubclass, require the parentUIViewControllerto be passed in — never try to create a parent yourself. - Prefer updating
rootViewover destroying/recreating the hosting controller for dynamic content.
FAQ
Can I use a SwiftUI view directly as a UIView without a view controller?
No. On iOS there is no public UIView subclass that renders SwiftUI. You must use UIHostingController and view controller containment. This is by design — the hosting controller manages the SwiftUI environment, layout passes, and UIKit integration.
Why does my SwiftUI view not resize when the parent UIView resizes?
Ensure the hosting controller’s view has Auto Layout constraints tied to its superview’s edges (or uses autoresizingMask if you’re not using Auto Layout). Also verify translatesAutoresizingMaskIntoConstraints = false when using constraints. Without proper constraints, the hosted view keeps its initial frame.
How do I pass data from UIKit to the embedded SwiftUI view?
Use an ObservableObject model shared between the UIKit side and the SwiftUI view. The UIKit code updates @Published properties; the SwiftUI view reads them via @ObservedObject or @StateObject. Alternatively, update hostingController.rootView with a new view that captures the latest values.
class SharedModel: ObservableObject {
@Published var title = "Initial"
}
// In UIKit:
model.title = "Updated"
// In SwiftUI:
struct ContentView: View {
@ObservedObject var model: SharedModel
var body: some View { Text(model.title) }
}
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.