๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UI

[iOS] UIKit์—์„œ SwiftUI Preview ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 7. 28.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

 

์–ผ๋งˆ ์ „์— ์œ ํŠœ๋ธŒ์—์„œ ๋งŒํ™”๊ฒฝ iOS ๊ฐœ๋ฐœ๊ธฐ๋ฅผ ๋ณด๋‹ค๊ฐ€ (https://www.youtube.com/watch?v=W_1FoBtaV7A)

 

UI๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š”๋ฐ ์Šคํ† ๋ฆฌ๋ณด๋“œ์™€ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์žฅ๋‹จ์ ์„ ๋ชจ๋‘ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š”๊ฒŒ UIKit์—์„œ Preview๋ฅผ

 

์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”.

 

Preview๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋กœ ํ™”๋ฉด์„ ๊ตฌํ˜„ํ•˜๋”๋ผ๋„ ์ง๊ด€์ ์œผ๋กœ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ๊ณ  ๋˜ํ•œ ์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ๊ฐœ๋ฐœํ•  ๋•Œ

 

์ƒ๊ธฐ๋Š” ์ถฉ๋Œ ๋ฌธ์ œ ๋“ฑ์„ ์—†์• ์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ !

 

๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ UIKit์—์„œ Preview๋ฅผ ์–ด๋–ป๊ฒŒ ์ ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~


UIViewController + Extension

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋ฆฌ๋ทฐ ์ต์Šคํ…์…˜์„ ๋งŒ๋“ค์–ด์ค˜์„œ ์–ด๋–ค ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์—์„œ๋“  ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

import SwiftUI

#if DEBUG
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

ViewController

 

๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ด๋™ํ•ด์„œ import SwiftUI๋ฅผ ํ•ด์ฃผ์‹œ๊ณ 

 

์•„๋ž˜์— PreviewProvider๋ฅผ ๋งŒ๋“ค์–ด์ค€ ๋’ค ํ•ด๋‹น ViewController๋ฅผ toPreview() ๋ฉ”์†Œ๋“œ๋กœ ํ”„๋ฆฌ๋ทฐ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

import SwiftUI

struct VCPreView:PreviewProvider {
    static var previews: some View {
        ViewController().toPreview()
    }
}

 

์Šคํ† ๋ฆฌ๋ณด๋“œ์ผ ๊ฒฝ์šฐ

 

์Šคํ† ๋ฆฌ๋ณด๋“œ ์•„์ด๋””๋ฅผ ์ •ํ•ด์ฃผ์‹œ๊ณ 

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ ์šฉํ•ด์ค๋‹ˆ๋‹ค.

 

struct VCPreView:PreviewProvider {
    static var previews: some View {
    	UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController").toPreview()
    }
}

 

์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•ด์ฃผ์‹œ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋ฆฌ๋ทฐ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚œ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”!

 

 


ํ”„๋ฆฌ๋ทฐ ์„ธ๋ถ€ ์„ค์ •ํ•˜๊ธฐ

 

ํ”„๋ฆฌ๋ทฐ ํ™”๋ฉด์—์„œ ์œ„์ชฝ์—์„œ ์•„๋ž˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด

 

์ด๋ฆ„,๋””๋ฐ”์ด์Šค,๋ ˆ์ด์•„์›ƒ,๋ฐฉํ–ฅ ๋“ฑ์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์•„๋ž˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์‹œ๋ฉด ๋””๋ฐ”์ด์Šค๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

 

 

ํ•œ๋ฒˆ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ์•„๋ž˜์™€ ๊ฐ™์ด ๋””๋ฐ”์ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

 

์œ„ ๋ฒ„ํŠผ๋“ค์„ ํด๋ฆญํ•˜๋ฉด ์ž๋™์œผ๋กœ  ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”.

 

struct VCPreView:PreviewProvider {
    static var previews: some View {
        Group {
            ViewController()
                .toPreview()
            .previewDevice("iphone 8")
            .previewDisplayName("์•„์ดํฐ 8")
            
            ViewController()
                .toPreview()
                .previewDevice("iPhone 12 Pro")
                .previewDisplayName("์•„์ดํฐ 12 ํ”„๋กœ")
                .preferredColorScheme(.dark)
        }
    }
}

 

์„œ๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„๊ณผ ๋””๋ฐ”์ด์Šค ๋‹คํฌ๋ชจ๋“œ ๋“ฑ์„ ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋ฆฌ๋ทฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ UIKit์—์„œ ํ”„๋ฆฌ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ž๋งˆ์ž ๋ฐ”๋กœ ํ™”๋ฉด์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ๋””๋ฐ”์ด์Šค์—์„œ ์–ด๋–ป๊ฒŒ ๋ณด์ด๋Š”์ง€ ๋ฐ”๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์„œ

 

ํ™•์‹คํžˆ ํŽธ๋ฆฌํ•˜๊ณ  ๊ฐœ๋ฐœ ์†๋„๋„ ํ–ฅ์ƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ํ”„๋ฆฌ๋ทฐ๊ฐ€ ๋””๋ฐ”์ด์Šค ์ ์šฉ๋˜๋Š” ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆด ๋•Œ๋„ ์žˆ๊ณ  ๋กœ๋”ฉ์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๋•Œ๋„ ์žˆ๊ณ  ํฌ๋ž˜์‰ฌ๋„ ์ž์ฃผ ๋‚˜๊ณ 

 

ํ•˜๋”๋ผ๊ตฌ์š”.. (์ด๊ฑด ์ ์  ๊ฐœ์„ ๋  ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!)

 

ํ˜น์‹œ๋ผ๋„ ๊ถ๊ธˆํ•˜์‹  ์  ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!


Reference

 

 

Use Xcode Previews with UIKit

Prerequisite: You will need macOS Catalina (10.15)+ and Xcode 11+ to run Xcode Previews.Apple introduced Xcode Previews in WWDC 2019 alongside with SwiftUI, which allow us to view UI changes immediately after each change, instead of needing to recompile th

fluffy.es

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€