7.9 · Hosting SwiftUI in UIKit and UIKit in SwiftUI
Real apps are mixed. SwiftUI screens sit inside UIKit navigation, and UIKit views you still need sit inside SwiftUI. Two small bridges carry everything.
Mental model. UIHostingController puts SwiftUI inside UIKit. UIViewRepresentable and UIViewControllerRepresentable put UIKit inside SwiftUI. The skill is knowing who owns the state.
The two bridges
Hosting SwiftUI, and wrapping UIKit.
- How do you put SwiftUI inside a UIKit app, and what do you need to watch for?
Wrap a SwiftUI view in a UIHostingController and use it as any other view controller: push it, present it, put it in a tab or embed it as a child with the… - How do UIViewRepresentable and its coordinator work, and when does SwiftUI call each method?
To use a UIKit view in SwiftUI, conform to UIViewRepresentable (or UIViewControllerRepresentable). makeUIView(context:) creates the view once…
More in UIKit
- 7.1 · Hit testing, the responder chain and gesture recognisers
- 7.2 · UIView and CALayer, frame, bounds and center
- 7.3 · View controller lifecycle, containment, presentation and transitions
- 7.4 · Navigation, tab, split and page controllers
- 7.5 · Compositional layout, diffable data sources, cell reuse and prefetching
- 7.6 · UITableView, still asked and still everywhere
- 7.7 · Trait collections, size classes, safe area, keyboard and appearance
- 7.8 · Keyboard shortcuts, pointer, Apple Pencil and multitasking
- 7.10 · Initialisers, stack views, scroll views and modern buttons
- 7.11 · Layout passes, retain cycles and menus
- 7.12 · Refreshing diffable cells, outlines and carousel layouts
- 7.13 · View-tree exercises and long navigation stacks