How to pass data from SwiftUI to uikit
Generated on 8/8/2024
1 search
To pass data from SwiftUI to UIKit, you can use the interoperability features provided by SwiftUI. Here are the steps and relevant details:
-
Using
UIViewRepresentable
: This protocol allows you to create and update a UIKit view from SwiftUI. You can pass data from SwiftUI to UIKit by updating the properties of theUIViewRepresentable
struct. -
Using
UIViewControllerRepresentable
: Similar toUIViewRepresentable
, this protocol is used for view controllers. You can pass data to the UIKit view controller by updating the properties of theUIViewControllerRepresentable
struct. -
Embedding SwiftUI in UIKit: You can use
UIHostingController
to embed a SwiftUI view in a UIKit view hierarchy. This allows you to pass data from SwiftUI to UIKit by setting the root view of theUIHostingController
.
Here are some relevant segments from the WWDC sessions:
-
SwiftUI essentials:
- "If there's a view or view controller from UIKit or AppKit that you want to use in SwiftUI, you can create a view representable. This is a special SwiftUI view protocol for creating and updating an associated UIKit or AppKit view using imperative code." SwiftUI essentials (21:25)
- "The inverse is true as well. If you want to embed a SwiftUI view into a UIKit or AppKit view hierarchy, you can use classes such as
HostingViewController
. This is created with a root SwiftUI view and can be added to your UIKit or AppKit view controller hierarchy." SwiftUI essentials (20:31)
-
What’s new in UIKit:
- "In iOS 18, you can now add your existing UIKit gesture recognizers directly to SwiftUI hierarchies by using the new
UIJs recognizer representable
protocol." What’s new in UIKit (04:22)
- "In iOS 18, you can now add your existing UIKit gesture recognizers directly to SwiftUI hierarchies by using the new
-
Enhance your UI animations and transitions:
- "Helping to power these new transitions are some amazing new lower-level APIs for animating UIKit and AppKit views with SwiftUI animations." Enhance your UI animations and transitions (07:49)
For more detailed information, you can refer to the sessions mentioned above.
What’s new in UIKit
Explore everything new in UIKit, including tab and document launch experiences, transitions, and text and input changes. We’ll also discuss better-than-ever interoperability between UIKit and SwiftUI animations and gestures, as well as general improvements throughout UIKit.
What’s new in AppKit
Discover the latest advances in Mac app development. Get an overview of the new features in macOS Sequoia, and how to adopt them in your app. Explore new ways to integrate your existing code with SwiftUI. Learn about the improvements made to numerous AppKit controls, like toolbars, menus, text input, and more.
Enhance your UI animations and transitions
Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.
SwiftUI essentials
Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.