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.

  1. How do you put SwiftUI inside a UIKit app, and what do you need to watch for? 7.9.1 · L3
    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…
  2. How do UIViewRepresentable and its coordinator work, and when does SwiftUI call each method? 7.9.2 · L3
    To use a UIKit view in SwiftUI, conform to UIViewRepresentable (or UIViewControllerRepresentable). makeUIView(context:) creates the view once…

More in UIKit