BlogiOS
Adding SwiftUI to a UIKit app without a rewrite
How to bring SwiftUI into a large UIKit codebase one screen at a time, and the traps to watch for.
Most iOS apps with real users were built in UIKit, and a full rewrite in SwiftUI is rarely worth the risk. You don’t have to choose. UIKit and SwiftUI work side by side, so you can build new screens in SwiftUI and leave the rest of the app alone.
I’ve done this on large apps, including new SwiftUI screens inside the existing UIKit app for Jumbo, one of the largest supermarket chains in the Netherlands. This is the approach I use.
Start with one leaf screen
Pick a screen at the end of a flow with few dependencies: a detail page, a settings screen or an onboarding step. Avoid the home screen or anything the rest of the app depends on. A small first screen lets the team learn SwiftUI on real code without putting the whole app at risk.
Host it with UIHostingController
UIHostingController wraps a SwiftUI view in a normal view controller, so the rest of the app doesn’t need to know SwiftUI is involved. To show a full screen, push it like any other view controller:
let detail = ProductDetailView(viewModel: viewModel)
let host = UIHostingController(rootView: detail)
host.title = product.name
navigationController?.pushViewController(host, animated: true)
To use SwiftUI for part of a screen, add the hosting controller as a child:
let host = UIHostingController(rootView: PromoBanner(promo: promo))
host.sizingOptions = .intrinsicContentSize // iOS 16+
addChild(host)
host.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(host.view)
NSLayoutConstraint.activate([
host.view.topAnchor.constraint(equalTo: containerView.topAnchor),
host.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
host.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
host.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
host.didMove(toParent: self)
Setting sizingOptions to .intrinsicContentSize lets Auto Layout use the size SwiftUI asks for, so you don’t have to hard-code heights. For table and collection view cells, iOS 16 added UIHostingConfiguration, which is simpler and sizes the cell for you:
cell.contentConfiguration = UIHostingConfiguration {
ProductRow(product: product)
}
Share state, don’t duplicate it
The most common mistake I see is a second copy of the data just for the SwiftUI screens. Keep your existing services and models as the single source of truth, and give the SwiftUI view a view model that wraps them:
final class CartViewModel: ObservableObject {
@Published private(set) var items: [CartItem] = []
private let cartService: CartService
init(cartService: CartService) {
self.cartService = cartService
items = cartService.items
}
func remove(_ item: CartItem) {
cartService.remove(item)
items = cartService.items
}
}
When UIKit creates the view model and passes it in, the SwiftUI view should hold it with @ObservedObject, because the view doesn’t own it. Use @StateObject only when the view creates the object itself. Mixing these up gives you screens that reset or reload whenever the parent redraws. On iOS 17 and later you can use the @Observable macro instead, and the same rule applies: the owner keeps it in @State, everyone else just receives it.
Keep navigation in UIKit, for now
Let SwiftUI views report what happened, and let UIKit decide where to go. Closures work well:
struct ProductDetailView: View {
@ObservedObject var viewModel: ProductDetailViewModel
var onAddToCart: (Product) -> Void
var body: some View {
Button("Add to cart") { onAddToCart(viewModel.product) }
}
}
The view controller or coordinator that pushed the screen handles onAddToCart. Don’t wrap a hosted screen in its own NavigationStack while it sits inside a UINavigationController, or you’ll get two navigation bars. Once most screens in a flow are SwiftUI, move that whole flow to NavigationStack in one step.
Make both worlds look the same
Users shouldn’t be able to tell which screens are SwiftUI. Define colours in the asset catalog so UIKit reads UIColor(named:) and SwiftUI reads Color("BrandPrimary") from the same place. Use semantic text styles like .body and .headline in both, so Dynamic Type works everywhere. Check spacing and corner radius against your existing components before the first screen ships.
Watch for these traps
- Deployment target. Many SwiftUI APIs depend on the iOS version. Check every API against your minimum supported version, not the one on your phone.
- Sizing in stack views and cells. Without
sizingOptionsorUIHostingConfiguration, hosted views often end up the wrong height. - Heavy work in
body. SwiftUI may callbodyoften. Keep formatting, sorting and image processing in the view model. - Tests. Keep logic in view models so XCTest covers it, and add XCUITest flows where UIKit and SwiftUI screens meet.
The payoff
Done this way, the app stays stable, the team keeps shipping features, and every new SwiftUI screen is cheaper than the last. Over time most new work lands in SwiftUI, and nobody had to stop for a rewrite.
Nishanth Kumar