SwiftUI with Coordinator Flow Controllers
SwiftUI is pretty nice, especially in the data binding between UI and data flow, is it possible to Integrate SwiftUI in a UIKit-based project?
I mean maybe some new view using SwiftUI and be presented or be pushed from an existing UIKit ViewController page, of course, it is possible, Apple provides elegant API for this scenario.
UIKit Present SwiftUI view or SwiftUI view present UIViewController
We can use UIHostingController to convert SwiftUI view to UIViewController and can be presented or push from an existing UIKit ViewController page.
In the FlowFactory
line 14, we use UIHostingController and set the parameter rootView with your SwiftUI view to make SwiftUI view can be presented or push from an existing UIKit ViewController
In the FlowController
line 6 and 7, factory.makeKeypadHostingController
will be called and return our SwiftUI view, which has already been converted to UIViewController. (In the Coordinator Flow controllers design pattern, all the navigation will be handled here).
Line 4 will be call while SwiftUI view need present other page, in this case we will present CNContactPickerViewController
let user select their contacts.
Using custom UIKit view in SwiftUI view
Sometimes we have some custom view which write with UIKit, and we need to use them in our SwiftUI view, let’s take Lottie for example. Create a view which conform UIViewRepresentable
then implement makeUIView and updateView method, now we can use LottieView in SwiftUI view.
Github:
https://github.com/codebbkaf/SwiftUI_Coordinator_FlowController