Include SwiftUI views in existing UIKit application
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The safest way to adopt SwiftUI in an older UIKit app is incrementally. UIHostingController lets a SwiftUI view behave like a normal UIKit view controller, so you can add new screens or components without rewriting the whole application shell.
Present a SwiftUI screen from UIKit
The simplest integration point is a full-screen push or modal presentation. Build a SwiftUI view, wrap it in UIHostingController, and present it like any other UIKit controller.
This works well for leaf screens where UIKit still owns navigation and app lifecycle.
Embed SwiftUI as one part of a UIKit screen
You do not need a full-screen transition to use SwiftUI. A hosting controller can also be added as a child controller inside an existing UIKit layout.
That pattern is useful for dashboards, settings pages, and card-style components that benefit from SwiftUI layout while UIKit still controls the surrounding screen.
Pass data from UIKit into SwiftUI
For immutable input, pass values directly through the SwiftUI view initializer. For shared mutable state, use an observable object and provide it to the view.
UIKit remains the owner of the shared object, while SwiftUI reacts to its published changes.
Send events back to UIKit
SwiftUI views can report user actions through closures, which is often simpler than building a delegate protocol for small integrations.
This keeps the ownership boundary clean: SwiftUI renders the interface, and UIKit decides what the action means inside the larger app flow.
Plan the migration boundary deliberately
Hybrid apps become messy when both frameworks try to own the same concern. A practical division is:
- UIKit owns navigation stacks, tab bars, and app lifecycle
- SwiftUI owns new leaf screens or isolated reusable components
- shared state lives in framework-neutral models or observable objects
That approach lets you modernize screen by screen without creating two competing app architectures.
Common Pitfalls
The most common mistake is forgetting proper child-controller containment when embedding SwiftUI in part of a UIKit screen. If you call addSubview without addChild and didMove, lifecycle behavior becomes inconsistent.
Another issue is passing mutable data by plain value when the SwiftUI view needs to react to updates. For dynamic state, use ObservableObject, bindings, or another explicit shared model.
Developers also underestimate lifecycle differences. onAppear in SwiftUI is related to view rendering, not a perfect one-to-one replacement for every UIKit appearance callback, so do not assume they fire under identical conditions.
Finally, avoid migrating navigation and presentation logic too early. Let UIKit continue owning the application shell until there is a clear reason to move more of the stack.
Summary
- Use
UIHostingControllerto present or embed SwiftUI inside an existing UIKit app. - Push hosted views as normal controllers or add them as child controllers inside UIKit layouts.
- Pass data through initializers or observable objects, and send actions back with closures.
- Keep ownership boundaries clear so UIKit and SwiftUI do not compete for the same responsibilities.
- Adopt SwiftUI incrementally rather than rewriting the application in one pass.
Related reading
- Indent the text in a UITextField
- Index of a substring in a string with Swift
- Ineligible Devices section appeared in Xcode 6.x.x
- initialize class method for classes in Swift?
- Initialize StateObject with a parameter in SwiftUI
- Initializer is inaccessable due to 'internal' protection level
- Initializer is inaccessable due to 'internal' protection level
- Input text dialog Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.