This is a simple project that shows how you can integrate a SwiftUIView into an existing UIKit project.
You can find my full article about the integration of both frameworks here.
I started this project in order to understand how SwiftUI and UIKit can be integrated together to get the best out of both of them. I made an animation with SwiftUI and then I embedded it into a UIKit project. This is actually just one of two projects that I worked on, you can find the "UIKit in SwiftUI" part here.
- macOS - 12.1
- Xcode - 13.3
First of all, create a SwiftUI view inside the project (I created a simple custom loading animation).
In order to bind it with the existing project, you'll need to place it inside a ContainerView, which you can add to the storyboard from the library.
You may have noticed that along with the ContainerView, a ViewController has also popped up, which in this case we'll remove because it needs to be replaced with the UIHostingController.
To do this, add a UIHostingController from the library, then connect the ContainerView to it with control-drag and then select "embed" from the menu that opens.
Now open the assistant and, first of all, import SwiftUI into the ViewController.
Then control-drag the segue you just created in the view controller and give it a name.
Use auto-completion to help you and lastly enter the name of your SwiftUIView as the root view.
Created by Barbara Beatrice