Hacker News new | ask | show | jobs
by Willamin 1484 days ago
Part of that is untrue. You can add SwiftUI to an app that starts with the UIKit lifecycle.

In fact, you can jump back and forth between UIKit and SwiftUI as many times as you like throughout your app's view hierarchy.

The current project I'm working on involves slowly porting an older UIKit+Storyboard app over to SwiftUI, so I'm seeing this in practice every day.

2 comments

What's involved in the "jumping back and forth"? Is it pretty cumbersome or no? Do you use a UINavigationController at all in your app, or do your UIViewControllers do all of the navigating/presenting themselves?
We do use UINagivationController in our app, so that's no issue.

Crossing the boundary from UIKit to SwiftUI involves instantiating the SwiftUIView, handing that over to a UIHostingController, then presenting that controller. Something like this:

  let myView = MySwiftUIView()
  let hostingVC = UIHostingController(rootView: myView)
  navigationController.present(hostingVC, animated: true)
Crossing the boundary from SwiftUI to UIKit is slightly more boilerplate to write, but it's not too bad. You have to implement either a UIViewControllerRepresentable or a UIViewRepresentable (depends on your needs). Those protocols only needs two functions defined: makeUIView/makeUIViewController and updateUIView/updateUIViewController. For the simplest views and view controllers, you can leave the update definitions empty.
Thanks, I didn't know that!