SwiftUI는 모든 Apple 플랫폼의 기존 UI 프레임워크와 원활하게 동작할 수 있다. UIKit의 뷰와 뷰컨트롤러를 SwiftUI의 뷰 안에 배치할 수 있으며 그 반대도 물론 가능하다.
SwiftUI에서 UIKit의 뷰와 뷰컨트롤러를 나타내려면
UIViewRepresentable
와UIViewControllerRepresentable
프로토콜을 준수하는 타입을 만들어야 한다. 커스텀 타입은 UIKit 타입으로 구성되고 만들어지지만 SwiftUI에 의해 생명 주기와 업데이트가 관리된다.
protocol UIViewRepresentable : View where Self.Body == Never
해당 뷰를 SwiftUI 뷰 계층 구조에 통합하는 데 사용하는 UIKit 뷰 전용 래퍼
UIViewRepresentable
and UIViewControllerRepresentable
types have the same life cycle, with methods that correspond to their underlying UIKit types.
protocol UIViewControllerRepresentable : View where Self.Body == Never
UIKit의 뷰컨트롤러를 나타내는 뷰
@MainActor func makeUIViewController(context: Self.Context) -> Self.UIViewControllerType
뷰컨트롤러 인스턴스를 만들고 초기 상태를 구성
SwiftUI는 뷰를 표시할 준비가 되었을 때 이 메서드를 한 번 호출한 다음 뷰 컨트롤러의 생명 주기를 관리
@MainActor func updateUIViewController(
_ uiViewController: Self.UIViewControllerType,
context: Self.Context
)