UIViewControllerRepresentable
Embedding full UIKit view controllers like UIImagePickerController in SwiftUI.
UIViewControllerRepresentable is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is UIViewControllerRepresentable?
Like UIViewRepresentable but wraps a full UIViewController for use inside SwiftUI.
struct ImagePickerView: UIViewControllerRepresentable {
@Binding var image: UIImage?
}makeUIViewController
Create and configure the UIKit view controller. Called once when SwiftUI first renders the representable.
func makeUIViewController(context: Context) -> UIImagePickerController {
let vc = UIImagePickerController()
vc.delegate = context.coordinator
return vc
}updateUIViewController
Called whenever SwiftUI state changes. Push updated values into the existing view controller.
func updateUIViewController(_ vc: UIImagePickerController, context: Context) {
// Update vc if needed (e.g., change source type)
}dismantleUIViewController
Static cleanup method called before the view controller is removed from the hierarchy.
static func dismantleUIViewController(_ vc: UIImagePickerController, coordinator: Coordinator) {
vc.dismiss(animated: false)
}Wrapping UIDocumentPickerViewController
Example: wrap document picker to let users select files and pass URLs back to SwiftUI.
struct DocumentPicker: UIViewControllerRepresentable {
@Binding var url: URL?
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf])
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ vc: UIDocumentPickerViewController, context: Context) {}
}Presenting Modally
Embed UIViewControllerRepresentable inside a .sheet for modal presentation.
@State private var showPicker = false
var body: some View {
Button("Pick Image") { showPicker = true }
.sheet(isPresented: $showPicker) {
ImagePickerView(image: $selectedImage)
}
}Mail Composer Example
Wrap MFMailComposeViewController to present a mail compose sheet from SwiftUI.
struct MailView: UIViewControllerRepresentable {
@Binding var isShowing: Bool
func makeUIViewController(context: Context) -> MFMailComposeViewController {
let vc = MFMailComposeViewController()
vc.mailComposeDelegate = context.coordinator
vc.setSubject("Hello from SwiftUI")
return vc
}
func updateUIViewController(_ vc: MFMailComposeViewController, context: Context) {}
}Full-Screen Coverage
Apply .fullScreenCover for view controllers that must fill the screen.
.fullScreenCover(isPresented: $showCamera) {
CameraViewController()
}Passing Closures for Callbacks
Instead of Bindings, pass closure callbacks to the representable for event notification.
struct VideoPlayer: UIViewControllerRepresentable {
let url: URL
var onFinish: () -> Void
// Coordinator calls onFinish when playback ends
}Intrinsic Size
UIKit view controllers determine their own size. Use SwiftUI frame modifiers or the view controller's preferred content size.
struct Popover: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
let vc = UIViewController()
vc.preferredContentSize = CGSize(width: 300, height: 200)
return vc
}
func updateUIViewController(_ vc: UIViewController, context: Context) {}
}Common Use Cases
UIViewControllerRepresentable is commonly used for: UIImagePickerController, MFMailComposeViewController, AVPlayerViewController, UIDocumentPickerViewController, and SFSafariViewController.
import SafariServices
struct SafariView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: Context) -> SFSafariViewController {
SFSafariViewController(url: url)
}
func updateUIViewController(_ vc: SFSafariViewController, context: Context) {}
}Quick Check
Which method is called by SwiftUI each time the representable's SwiftUI inputs change?
Lesson Recap
UIViewControllerRepresentable wraps full UIKit view controllers. Implement makeUIViewController (create once), updateUIViewController (sync state), and optionally dismantleUIViewController (cleanup). Use Coordinator for delegate callbacks.
Frequently asked questions
Is the “UIViewControllerRepresentable” lesson free?
Yes — the full text of “UIViewControllerRepresentable” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “UIViewControllerRepresentable”?
Embedding full UIKit view controllers like UIImagePickerController in SwiftUI. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “UIViewControllerRepresentable” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.