Member-only story
SwiftUI: UIViewRepresentable
Learn about UIViewRepresentable in Swift and SwiftUI
So, you’ve been loving SwiftUI. Declarative syntax, easy previews, and smooth animations. But then, reality strikes. Your app needs a UIKit component that doesn’t exist in SwiftUI. Maybe it’s MKMapView for maps, WKWebView for web content, or some third-party UIKit view.
This is where UIViewRepresentable comes in. It’s SwiftUI’s secret handshake with UIKit, allowing you to wrap UIKit views and make them work seamlessly in a SwiftUI environment.
Bringing UIKit to SwiftUI
Let’s say we’re building “ThrowbackTube,” an app that lets users watch 2010–2019 viral videos. You want to use WKWebView (a UIKit component) to display embedded YouTube videos. But SwiftUI doesn’t have a native WebView. That’s where UIViewRepresentable saves the day.
Step 1: Create a Struct Conforming to UIViewRepresentable
A UIViewRepresentable struct serves as the middleman between UIKit and SwiftUI.
Here’s what’s happening:
makeUIView(context:)…









