Search
📱

UIKit 에서 Canvas 사용하기

부제
카테고리
UIKit
세부 카테고리
스토리보드 없이 개발하기
Combine 카테고리
최종편집일
2022/09/20 08:14
작성중
관련된 포스팅
생성 일시
2022/07/16 14:26
태그
안녕하세요 iOS 개발자 루크입니다~!
스토리보드 없이 개발을 하려다 보면
하루에 몇백번은 빌드를 해보는 것 같습니다 ~.~
그래서 오늘은 그러한 수고를 덜어드릴 수 있도록
SwiftUI 에서 사용하는 기능인 canvas 를 UIKit 에 적용해보도록 하겠습니다.
이 canvas 가 있으면 바로바로 UI 를 확인이 가능하답니다!

UIViewContollerRepresentable

원래는 UIKit 으로 만들어진 UIView 를 SwfitUI 에서 사용하기위한 프로토콜입니다만,
오늘은 조금 다른 방식으로 사용해보겠습니다.

1단계 : SwiftUI 임포트해주기

import SwiftUI
Swift
복사
다만 이 코드는 SwiftUI 로 오해받을 여지가 있기 때문에 ViewController 하단에 작성해주었습니다

2단계 : UIViewContollerRepresentable 채택 , 준수

struct ViewControllerRepresentable: UIViewControllerRepresentable { typealias UIViewControllerType = ViewController func makeUIViewController(context: Context) -> ViewController { return ViewController() } func updateUIViewController(_ uiViewController: ViewController, context: Context) { } }
Swift
복사

3단계 : canvas 에 preview 띄우기

@available(iOS 13.0.0, *) struct ViewPreview: PreviewProvider { static var previews: some View { ViewControllerRepresentable() } }
Swift
복사

정리

import SwiftUI struct ViewControllerRepresentable: UIViewControllerRepresentable { typealias UIViewControllerType = 뷰컨트롤러클래스명 func makeUIViewController(context: Context) -> ViewController { return 뷰컨트롤러클래스명() } func updateUIViewController(_ uiViewController: ViewController, context: Context) { } } @available(iOS 13.0.0, *) struct ViewPreview: PreviewProvider { static var previews: some View { ViewControllerRepresentable() } }
Swift
복사