Open samsung-ga opened 2 years ago
UIHostingController
import UIKit import SwiftUI // ✅ ContentView는 스유 class ViewController: UIViewController { let contentView = UIHostingController(rootView: ContentView()) override func viewDidLoad() { super.viewDidLoad() addChild(contentView) view.addSubview(contentView.view) contentView.view.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ contentView.view.topAnchor.constraint(equalTo: view.topAnchor), contentView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor), contentView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), contentView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) } }
UIViewControllerRepresentable
UIViewControllerType을 이용하는 방법
UIViewControllerType
Coordinator
import PhotosUI import SwiftUI // ✅ PHPickerViewController에 대한 예제 struct ImagePicker: UIViewControllerRepresentable { @Binding var image: UIImage? func makeUIViewController(context: Context) -> PHPickerViewController { var config = PHPickerConfiguration() config.filter = .images let picker = PHPickerViewController(configuration: config) picker.delegate = context.coordinator return picker } func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { } typealias UIViewControllerType = PHPickerViewController func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, PHPickerViewControllerDelegate { var parent: ImagePicker init(_ parent: ImagePicker) { self.parent = parent } func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true) guard let provider = results.first?.itemProvider else { return } if provider.canLoadObject(ofClass: UIImage.self) { provider.loadObject(ofClass: UIImage.self) { image, _ in self.parent.image = image as? UIImage } } } } }
UIViewControllerType을 이용하지 않는 방법
import Foundation import SwiftUI struct StoryboardViewController: UIViewControllerRepresentable { @Binding var text: String? func makeUIViewController(context: Context) -> some UIViewController { let storyboardName = String(describing: "ChildViewController") let storyboard = UIStoryboard.init(name: storyboardName, bundle: nil) let controller = storyboard.instantiateViewController(identifier: storyboardName) guard let child = controller as? ChildViewController else { fatalError() } child.delegate = self return controller } func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { } } extension StoryboardViewController: ChildDelegate { func pressButton() { text = "프로토콜을 이용한 pressButton" } }
UIKit in SwiftUI
SwiftUI in UIKit
SwiftUI in UIKit
UIHostingController
이용UIKit in SwiftUI
UIViewControllerRepresentable
이용UIViewControllerType을 이용하는 방법
UIViewControllerRepresentable
프로토콜을 준수하는 struct 타입을 생성한다.UIViewControllerType
을 지정해주고 필수 메소드 2개를 구현한다.Coordinator
를 이용한다. (UIKit의 Coordinator 패턴과는 전혀 다른 개념이니까 조심!)UIViewControllerType을 이용하지 않는 방법
Reference
UIKit in SwiftUI
SwiftUI in UIKit