One Logo Pool Ball
SwiftUI · IOS API REFERENCE

UIViewRepresentable

protocol · iOS 13.0+ · SDK 27.1

@MainActor @preconcurrency protocol UIViewRepresentable : View where Self.Body == Never

A wrapper for a UIKit view that you use to integrate that view into your SwiftUI view hierarchy. Use a ``UIViewRepresentable`` instance to create and manage a <doc://com.apple.documentation/documentation/UIKit/UIView> object in your SwiftUI interface. Adopt this protocol in one of your app's custom instances, and use its methods to create, update, and tear down your view. The creation and update processes parallel the behavior of SwiftUI views, and you use them to configure your view with your app's current state information. Use the teardown process to remove your view cleanly from your SwiftUI. For example, you might use the teardown process to notify other objects that the view is disappearing. To add your view into your SwiftUI interface, create your ``UIViewRepresentable`` instance and add it to your SwiftUI interface. The system calls the methods of your representable instance at appropriate times to create and update the view. The following example shows the inclusion of a custom `MyRepresentedCustomView` structure in the view hierarchy. struct ContentView: View { var body: some View { VStack { Text("Global Sales") MyRepresentedCustomView() } } } The system doesn't automatically communicate changes occurring within your view to other parts of your SwiftUI interface. When you want your view to coordinate with other SwiftUI views, you must provide a ``NSViewControllerRepresentable/Coordinator`` instance to facilitate those interactions. For example, you use a coordinator to forward target-action and delegate messages from your view to any SwiftUI views. - Warning: SwiftUI fully controls the layout of the UIKit view's <doc://com.apple.documentation/documentation/UIKit/UIView/center>, <doc://com.apple.documentation/documentation/UIKit/UIView/bounds>, <doc://com.apple.documentation/documentation/UIKit/UIView/frame>, and <doc://com.apple.documentation/documentation/UIKit/UIView/transform> properties. Don't directly set these layout-related properties on the view managed by a `UIViewRepresentable` instance from your own code because that conflicts with SwiftUI and results in undefined behavior.

Members (2)

get · iOS 13.0+

body

Swift declaration
@MainActor @preconcurrency var body: Never { get }

Declares the content and behavior of this view.

method · iOS 13.0+

makeCoordinator

Swift declaration
@MainActor @preconcurrency func makeCoordinator() -> Self.Coordinator

Creates the custom instance that you use to communicate changes from your view to other parts of your SwiftUI interface. Implement this method if changes to your view might affect other parts of your app. In your implementation, create a custom Swift instance that can communicate with other parts of your interface. For example, you might provide an instance that binds its variables to SwiftUI properties, causing the two to remain synchronized. If your view doesn't interact with other parts of your app, providing a coordinator is unnecessary. SwiftUI calls this method before calling the ``UIViewRepresentable/makeUIView(context:)`` method. The system provides your coordinator either directly or as part of a context structure when calling the other methods of your representable instance.

Skipped declarations

2 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

UIViewRepresentable.dismantleUIView(_:coordinator:)

Static protocol requirements cannot be called through an instance.

UIViewRepresentable.sizeThatFits(_:uiView:context:)

The iOS SDK compiler rejected this binding: cannot access associated type 'UIViewType' from 'UIViewRepresentable'; use a concrete type or generic parameter base instead

All SwiftUI types →