0Pricing
Swift Academy · 강의

UIViewControllerRepresentable

UIImagePickerController 같은 전체 UIKit 뷰 컨트롤러를 SwiftUI에 포함합니다.

UIViewControllerRepresentable은(는) CoddyKit의 무료 Swift Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Swift Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Swift Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

UIViewControllerRepresentable이란 무엇인가요

UIViewRepresentable과 비슷하지만 SwiftUI 내부에서 사용할 전체 UIViewController를 래핑합니다.

struct ImagePickerView: UIViewControllerRepresentable {
  @Binding var image: UIImage?
}

makeUIViewController

UIKit 뷰 컨트롤러를 만들고 구성합니다. SwiftUI가 representable을 처음 렌더링할 때 한 번 호출됩니다.

func makeUIViewController(context: Context) -> UIImagePickerController {
  let vc = UIImagePickerController()
  vc.delegate = context.coordinator
  return vc
}

updateUIViewController

SwiftUI 상태가 변경될 때마다 호출됩니다. 업데이트된 값을 기존 뷰 컨트롤러에 전달하세요.

func updateUIViewController(_ vc: UIImagePickerController, context: Context) {
  // Update vc if needed (e.g., change source type)
}

dismantleUIViewController

뷰 컨트롤러가 계층 구조에서 제거되기 전에 호출되는 정적 정리 메서드입니다.

static func dismantleUIViewController(_ vc: UIImagePickerController, coordinator: Coordinator) {
  vc.dismiss(animated: false)
}

UIDocumentPickerViewController 래핑하기

예를 들어 문서 picker를 래핑하여 사용자가 파일을 선택하고 URL을 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) {}
}

모달로 표시하기

모달로 표시하려면 UIViewControllerRepresentable을 .sheet 안에 넣으세요.

@State private var showPicker = false
var body: some View {
  Button("Pick Image") { showPicker = true }
    .sheet(isPresented: $showPicker) {
      ImagePickerView(image: $selectedImage)
    }
}

메일 작성기 예제

MFMailComposeViewController를 래핑하여 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) {}
}

전체 화면 표시

화면 전체를 채워야 하는 뷰 컨트롤러에는 .fullScreenCover를 적용하세요.

.fullScreenCover(isPresented: $showCamera) {
  CameraViewController()
}

콜백에 클로저 전달하기

바인딩 대신 representable에 클로저 콜백을 전달하여 이벤트를 알릴 수 있습니다.

struct VideoPlayer: UIViewControllerRepresentable {
  let url: URL
  var onFinish: () -> Void
  // Coordinator calls onFinish when playback ends
}

고유 크기

UIKit 뷰 컨트롤러는 자체 크기를 결정합니다. SwiftUI 프레임 수정자나 뷰 컨트롤러의 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) {}
}

일반적인 사용 사례

UIViewControllerRepresentable은 일반적으로 UIImagePickerController, MFMailComposeViewController, AVPlayerViewController, UIDocumentPickerViewController, SFSafariViewController에 사용됩니다.

import SafariServices
struct SafariView: UIViewControllerRepresentable {
  let url: URL
  func makeUIViewController(context: Context) -> SFSafariViewController {
    SFSafariViewController(url: url)
  }
  func updateUIViewController(_ vc: SFSafariViewController, context: Context) {}
}

빠른 확인

representable의 SwiftUI 입력이 변경될 때마다 SwiftUI가 호출하는 메서드는 무엇인가요?

강의 요약

UIViewControllerRepresentable은 전체 UIKit 뷰 컨트롤러를 래핑합니다. makeUIViewController(한 번 생성), updateUIViewController(상태 동기화), 선택 사항인 dismantleUIViewController(정리)를 구현하세요. 델리게이트 콜백에는 Coordinator를 사용하세요.

자주 묻는 질문

“UIViewControllerRepresentable” 강의는 무료인가요?

네 — “UIViewControllerRepresentable” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Swift Academy 강의 전체를 잠금 해제할 수 있습니다. Swift Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“UIViewControllerRepresentable”에서 뭘 배우나요?

UIImagePickerController 같은 전체 UIKit 뷰 컨트롤러를 SwiftUI에 포함합니다. 브라우저에서 직접 실행하는 실습 코드로 Swift Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Swift Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Swift Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“UIViewControllerRepresentable” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Swift Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Swift Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. UIViewRepresentable: UIKit 뷰 래핑하기
  2. 델리게이트 통신을 위한 코디네이터 패턴
  3. UIViewControllerRepresentable
  4. UIHostingController: UIKit 안의 SwiftUI
← Swift Academy(으)로 돌아가기