Sitemap
Stanley Miller 

Stanley Miller 

iOS Developer. Author. Fan of the interesting. Written 100+ Swift/SwiftUI articles.

Member-only story

SwiftUI: UIViewRepresentable

Learn about UIViewRepresentable in Swift and SwiftUI

3 min readMar 12, 2025

--

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

Here’s what’s happening:

  • makeUIView(context:)…

--

--

Stanley Miller 
Stanley Miller 

Written by Stanley Miller 

iOS Developer. Author. Fan of the interesting. Written 100+ Swift/SwiftUI articles.