When bringing SwiftUI into a large UIKit shopping app, how do you choose between hosting whole screens and bridging components bottom-up, and who owns each boundary?
answer
- screens or leaves first
- navigation stays with one owner
- the boundary is an API
- dependencies injected at the host
- each wrapper has an owner
basics
~20 sHost whole new screens in UIHostingController when a flow can stay UIKit-navigated, and bridge leaf components with UIHostingConfiguration or representables when screens are shared. Either way, one team owns each boundary type and its contract.
solid answer
~40 sThere are two seams. Top-down, new or rewritten screens are SwiftUI views pushed in `UIHostingController`s while `UINavigationController` keeps owning navigation; this gives the most SwiftUI per change, but every hosted screen needs its dependencies injected because it starts a new SwiftUI hierarchy. Bottom-up, SwiftUI enters as rows through `UIHostingConfiguration` or as embedded child hosting controllers inside existing UIKit screens, which is low risk but leaves UIKit owning layout and navigation longer. Going the other way, legacy UIKit components the SwiftUI screens still need are wrapped once in representables. I would pick per area by how much of a flow changes, account for the iOS 16 requirement of cell hosting, and give each boundary an owner: a platform team owns the hosting factory and shared wrappers, feature teams own their screens.
code
swift · 20 linesimport Observation
import SwiftUI
import UIKit
@Observable
final class Cart {
var items: [String] = []
}
struct AppDependencies {
let cart: Cart
}
/// The one place where UIKit code turns a SwiftUI screen into a view controller.
@MainActor
func hostedScreen(_ screen: some View, title: String, dependencies: AppDependencies) -> UIViewController {
let host = UIHostingController(rootView: screen.environment(dependencies.cart))
host.title = title
return host
}go deeper
Recall the two directions: UIHostingController and UIHostingConfiguration bring SwiftUI into UIKit, representables bring UIKit into SwiftUI.
Explain how each seam changes who owns layout and navigation in a screen, and why hosted screens need dependencies injected.
Show how to keep mixed flows debuggable: one navigation owner, a shared hosting factory, wrappers that follow the make-update-dismantle rules.
Own the order of migration and the boundary contracts: which seam per area, who maintains shared wrappers, and when bridging gives way to a rewrite.
## The decision A ten-year-old UIKit shopping app wants SwiftUI: the team expects faster screen work and wants to stop writing new UIKit. Nobody rewrites such an app at once, so SwiftUI arrives through the **bridging boundary**, and the lead must decide where that boundary sits in each area and who maintains it. There is no single right answer; the choice depends on how much of a flow is changing and how many teams touch it. ## The two seams | | Top-down: host whole screens | Bottom-up: bridge components | |---|---|---| | How SwiftUI enters | New or rewritten screens pushed in `UIHostingController` | Rows through `UIHostingConfiguration`, sections as child hosting controllers | | Who owns navigation | The existing `UINavigationController` | Unchanged UIKit screens | | Who owns layout | SwiftUI, inside each hosted screen | UIKit, with SwiftUI islands | | Risk per change | Medium: a whole screen changes | Low: one row or section changes | | SwiftUI gained per change | High | Low | | Version floor | iOS 13 for hosting; sizing options iOS 16 | iOS 16 for cell hosting | The opposite direction appears in both: SwiftUI screens that still need a mature UIKit component, such as a custom map view, a camera controller or a rich-text editor, wrap it once in a `UIViewRepresentable` or `UIViewControllerRepresentable` instead of rewriting it. ## Choosing per area A practical rule of thumb, not a law: - **New flows** (a new checkout, a new onboarding) start as hosted SwiftUI screens; there is no UIKit to preserve. - **Stable, heavily used UIKit screens** take SwiftUI bottom-up, as rows or sections, when they need visual changes; their navigation, data sources and analytics stay put. - **Areas where most screens are already SwiftUI** stop bridging and move to a SwiftUI flow hosted once at its entry point, because by then the bridges cost more than they save. - **The deployment target** limits the options: if the app still supports iOS 15, cell hosting with `UIHostingConfiguration` and `sizingOptions` work only behind `if #available(iOS 16, *)` with a UIKit fallback, so hosting whole screens is the one seam that covers every supported release. ## The boundary as an API Every crossing has a contract, and most migration bugs are contract bugs: 1. **Dependencies.** A hosting controller starts a **new SwiftUI hierarchy**, so environment values and objects other SwiftUI screens installed are not there; Apple's documentation says reading a missing object by type makes SwiftUI throw an exception. One **hosting factory** should attach what every hosted screen expects, for example the cart through `environment(_:)` (the `Observable` form is iOS 17). 2. **Traits.** Values derived from UIKit traits, such as color scheme and Dynamic Type size, cross automatically. App-specific values that both worlds need can be bridged with a custom trait and a `UITraitBridgedEnvironmentKey` (iOS 17), which reads the value from a trait collection and writes it into mutable traits. 3. **Navigation.** One owner per stack. While UIKit pushes hosted screens, those screens should not also run their own SwiftUI navigation containers for the same stack; events such as "show product" go out through closures and the UIKit side pushes. 4. **Wrappers.** Each representable follows the same rules: configuration in `makeUIView`, all state in `updateUIView` with echo guards for two-way values, the coordinator as the only delegate, cleanup in `dismantleUIView`. ## Who owns what - **A platform or app-infrastructure team** owns the hosting factory, the bridged trait keys and the shared representable wrappers. Feature teams consume them rather than writing their own variants, because ten slightly different hosting helpers produce ten different environment bugs. - **Feature teams** own their SwiftUI screens and the closures those screens expose to UIKit. - **Reviews** check boundary changes specifically: a new dependency in a hosted screen must be added to the factory, and a new two-way representable must show its echo guard. ## Signals to watch - **Bridge density.** Counting hosting controllers and representables per flow shows when an area should switch from bridging to a rewrite. - **Crash reports from missing environment objects**, which point to a hosted screen created outside the factory. - **Layout bugs at embedded hosting controllers**, which usually mean a missing sizing option or missing child containment. ## Trade-offs a lead should name - Top-down gives more SwiftUI faster but changes more per release and pushes dependency wiring to the boundary. - Bottom-up is lower risk and incremental but keeps UIKit owning layout and navigation longer, and every island is another boundary to maintain. - Wrapping UIKit components avoids rewrites but leaves code that needs someone who knows both frameworks to change it. Moving the app's entry point itself to the SwiftUI app life cycle is a separate decision with its own plan; the seams above work while UIKit still owns the window.
- Why keep navigation owned by UIKit while screens are hosted, rather than mixing navigation models?A hosted screen pushed by `UINavigationController` keeps one source of truth for the back stack, deep links and state restoration. If hosted screens also run their own SwiftUI navigation containers inside the UIKit stack, two systems can each claim the back button and the path, and bugs tend to appear at the hand-offs.
- What signals that a UIKit area should be rewritten as a whole rather than bridged further?When most of a flow's screens are already SwiftUI and the remaining UIKit pieces are only reached through representables and hosting factories, the bridges cost more than they save. Rewrite the flow, including its navigation, as SwiftUI and host it once at its entry point.
- Planning to rewrite the whole app in SwiftUI in one release instead of choosing a seam per area.
- Letting each feature team write its own hosting and dependency wiring.
- Running SwiftUI navigation inside hosted screens while UIKit also pushes them.
- Assuming hosted screens inherit the app's SwiftUI environment automatically.
- Planning cell hosting and sizingOptions for an app whose oldest supported release is iOS 15, with no fallback.