Skip to content

BarbaraBeatrice/SwiftUI-in-UIKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

SwiftUI in UIKit

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.

gif appù

Table of Contents

General Information

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.

Technologies Used

  • macOS - 12.1
  • Xcode - 13.3

Embed a SwiftUI view in UIKit

First of all, create a SwiftUI view inside the project (I created a simple custom loading animation).

gif animazione

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. image

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. image

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. image image

Now open the assistant and, first of all, import SwiftUI into the ViewController. image

Then control-drag the segue you just created in the view controller and give it a name. image

Use auto-completion to help you and lastly enter the name of your SwiftUIView as the root view. image

Contact

Created by Barbara Beatrice

Releases

No releases published

Packages

No packages published

Languages