본문 바로가기

𝗫-𝗖𝗢𝗗𝗘

Xcode | UIKit 미리보기(Preview) 기능

 

 

 

 

출처: Pinterest

 

 

 

 

먼저, UIKit에서 SwiftUI처럼 미리보기(Preview)를 사용하고 싶으면 위 사진과 같이 SwiftUI 파일을 만들어줘야 합니다.

 

 

파일 생성 후 아래의 코드를 붙여넣어 주세요.

 

#if DEBUG
import SwiftUI

extension UIViewController {
    private struct Preview: UIViewControllerRepresentable {
            let viewController: UIViewController

            func makeUIViewController(context: Context) -> UIViewController {
                return viewController
            }

            func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
            }
        }

        func toPreview() -> some View {
            Preview(viewController: self)
        }
}
#endif

 

 

위 사항을 모두 진행하셨다면 아래의 코드를 미리보기(Preview)를 사용하고자 하는 ViewController에 추가해주세요.

 

#if DEBUG
import SwiftUI

struct VCPreView: PreviewProvider {
    static var previews: some View {
        파일이름().toPreview()
    }
}
#endif

 

 

이때,

 

파일이름().toPreview()

 

이 부분에 들어갈 파일의 이름은 미리보기(Preview)를 사용하고자하는 ViewController의 이름을 기재해주셔야 합니다.

 

 

모든 코드를 문제없이 작성하셨다면 아래의 단축키를 이용해 미리보기(Preview)를 사용하실 수 있습니다.

 

▶ 새로고침: Command + Option + P

▶ Canvas 열기 / 닫기: Command + Option + Enter

 

 

 

 

 

 

 

 

 

미리보기 적용 화면

 

 

 

 

 

Recent Posts
Visits
Today
Yesterday
Archives
Calendar
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31