0Pricing
Swift Academy · Lesson

UIViewRepresentable: Wrapping UIKit Views

Implementing makeUIView, updateUIView and dismantling UIKit views in SwiftUI.

Why UIViewRepresentable?

Many UIKit views have no SwiftUI equivalent yet. UIViewRepresentable bridges UIKit views into SwiftUI.

import SwiftUI
import UIKit

struct WebView: UIViewRepresentable {
  let url: URL
  // ...
}

Required Methods

Implement makeUIView to create the view and updateUIView to sync SwiftUI state changes.

func makeUIView(context: Context) -> WKWebView {
  WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
  uiView.load(URLRequest(url: url))
}

All lessons in this course

  1. UIViewRepresentable: Wrapping UIKit Views
  2. Coordinator Pattern for Delegate Communication
  3. UIViewControllerRepresentable
  4. UIHostingController: SwiftUI Inside UIKit
← Back to Swift Academy