tezvyn:

UIViewRepresentable: Bridging UIKit and SwiftUI

AI-drafted, machine-checkedSource: developer.apple.comadvanced
UIViewRepresentable: Bridging UIKit and SwiftUI

UIViewRepresentable is an adapter that lets you use battle-tested UIKit views inside your modern SwiftUI code. Use it for components like WKWebView that lack a native SwiftUI equivalent.

WHY IT EXISTS: SwiftUI is a modern UI framework, but UIKit has a vast library of mature, battle-tested components developed over more than a decade. Rewriting complex views like web browsers or intricate custom controls is often impractical. UIViewRepresentable exists to bridge this gap, allowing developers to leverage existing UIKit code within new SwiftUI applications.

THE MENTAL MODEL: Think of UIViewRepresentable as a special-purpose adapter or a container. You place an existing UIKit UIView inside this container. SwiftUI manages the container's size and lifetime, while the adapter's job is to translate declarative SwiftUI state changes into imperative commands that the old-school UIView can understand.

HOW IT WORKS: You create a struct that conforms to the UIViewRepresentable protocol. You must implement two key methods: makeUIView(context:), which is called only once to create and configure your UIView instance, and updateUIView(_:context:), which is called whenever SwiftUI state that affects the view changes. For communication from the UIView back to SwiftUI (like user input or delegate events), you create a nested Coordinator class. This coordinator acts as the UIView's delegate or target, capturing events and updating SwiftUI state via bindings.

WHEN TO USE IT: Use it when a native SwiftUI component is unavailable or insufficient for your needs. Prime candidates are WKWebView for displaying web content, MKMapView for advanced map customizations, or integrating third-party SDKs that only provide UIKit components. It is also the primary tool for incrementally migrating a large UIKit application to SwiftUI without a complete rewrite.

WHEN NOT TO USE IT: Avoid this protocol for simple views that have a direct SwiftUI counterpart. If you can build your component using SwiftUI's native Text, Image, Button, and layout stacks, that is the preferred approach. Using UIViewRepresentable for simple cases adds unnecessary complexity and boilerplate, working against the declarative and concise nature of SwiftUI.

ONE CANONICAL EXAMPLE: A common use case is wrapping a UISlider. The makeUIView method would create the UISlider. The updateUIView method would sync the slider's value property from a SwiftUI @State variable. To get the value back, a Coordinator would be assigned as the target for the slider's .valueChanged event. When the user moves the slider, the coordinator's action method fires and updates a @Binding passed in from the SwiftUI view, enabling true two-way data binding.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.