๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] SwiftUI๋ž€? ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• !

ibelieveinme 2023. 7. 7. 10:23
728x90

*SwiftUI: Apple์˜ ์ƒˆ๋กœ์šด UI ํ”„๋ ˆ์ž„์›Œํฌ๋กœ Declarative Swift Code ๋กœ ๋งŒ๋“ค์–ด์ง.

 

์ดˆ๋ณด์ž๋“ค์„ ์œ„ํ•ด iOS ์•ฑ์˜ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค๊ธฐ ์‰ฝ๋„๋ก ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ ์ฝ”๋“œ ์ƒ์„ฑ ๊ธฐ๋Šฅ์„ ๋งŒ๋“  ๊ฒƒ.

์บ”๋ฒ„์Šค์— ๊ทธ๋ƒฅ ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญํ•˜๋ฉด ํ•„์š”ํ•œ ์ฝ”๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ทฐ๊ฐ€ ์ œ๊ณต๋˜์–ด ํ™”๋ฉด์—์„œ ๋ฏธ๋ฆฌ ๋ณด๊ณ  ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

UI๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ๊ฒŒ ๊ฐ€์žฅ ํฐ ์žฅ์  !

 

SwiftUI๋ฅผ ๋งŒ๋“  ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ์• ํ”Œ์˜ ํฌ๋กœ์Šค ํ”Œ๋žซํผ(์›Œ์น˜, ๋งฅ, iPhone) ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

Project Catalyst๋กœ Mac ์•ฑ์œผ๋กœ ๋ฐ”๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

iOS 14 ์ด์ƒ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์น˜์—์„œ๋งŒ ๊ตฌ๋™๊ฐ€๋Šฅํ•จ. iPhone 6S ์ด์ƒ. iPad Air2 ์ด์ƒ. 

 

https://developer.apple.com/kr/xcode/swiftui/

 

SwiftUI ๊ฐœ์š” - Xcode - Apple Developer

SwiftUI๋Š” Swift์˜ ์„ฑ๋Šฅ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ชจ๋“  Apple ํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ํ˜์‹ ์ ์ด๊ณ  ๊ฐ„์†Œํ™”๋œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

developer.apple.com


*SwiftUI ์‚ฌ์šฉ๋ฒ•

1. SwiftUI Interface๋กœ ์ƒˆ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

New Project ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ Interface ๋ฅผ storyboard ๊ฐ€ ์•„๋‹Œ SwiftUI ๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.

 

์ดˆ๊ธฐ ์…‹ํŒ…์ฐฝ์€ ํ”„๋กœ์ ํŠธ ํด๋”, ์ฝ”๋“œ, ๋ผ์ด๋ธŒ ํ”„๋ฆฌ๋ทฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

 

Resume ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด build ๋ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์œ„์— ํ™”๋ฉด ์กฐ์ž‘๋ฒ„ํŠผ ๋ˆ„๋ฅด๋‹ค๊ฐ€ ๊ฐ‘์ž๊ธฐ Canvas ๊ฐ€ ์‚ฌ๋ผ์กŒ์—ˆ๋Š”๋ฐ, Canvas ์ฒดํฌํ•˜๊ณ  Layout > Canvas on Right ์ ์šฉํ•ด์ฃผ๋‹ˆ ์˜†์— ์ž˜ ์ƒ๊ฒผ๋‹ค.

 

2. ํ”„๋ฆฌ๋ทฐ ๊ธฐ๊ธฐ ๋ณ€๊ฒฝ

1) ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .previewDevice("iPhone 11")
    }
}

ContentView().previewDevice("iPhone 11") ์œผ๋กœ ContentView()์— previewDevice๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

2) Content View ์˜ Device ๋ณ€๊ฒฝ

Content View ์˜ Device ๋ณ€๊ฒฝ์œผ๋กœ๋„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๊ฐ€ ์„ค์น˜๋œ ๋‹จ๋ง๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ์—†์œผ๋ฉด ์ถ”๊ฐ€ ๋‹ค์šด๋กœ๋“œํ•ด์ฃผ๊ธฐ.

 

3. UI์š”์†Œ ์ถ”๊ฐ€ํ•˜๊ธฐ

1) ๋ฒ„ํŠผ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๊ธฐ

storyboard ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ ์ƒ๋‹จ์— + ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  Object ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์—ฐ๋‹ค.

๋งˆ์šฐ์Šค๋กœ preview ๋‚˜ ์ฝ”๋“œ์— ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญํ•˜๋ฉด ์ƒ์„ฑ & ๋ฐฐ์น˜๋œ๋‹ค.

ํ™”๋ฉด ์œ„๋กœ ์ฐจ๊ณก์ฐจ๊ณก ์Œ“์•„์•ผํ•  ๋•Œ๋Š” ZStack ์„ ์ด์šฉํ•ด์ฃผ์ž.

 

ZStack | Apple Developer Documentation

A view that overlays its subviews, aligning them in both axes.

developer.apple.com

์ฐธ๊ณ ๋กœ SwiftUI ์˜ Stack ์—๋Š” V, H, Z Stack ์ด ์žˆ๋‹ค. VStack(์ˆ˜์ง์œผ๋กœ ๋ฐฐ์—ด), HStack(์ˆ˜ํ‰์œผ๋กœ ๋ฐฐ์—ด), ZStack(ํ™”๋ฉด ์œ„๋กœ ์Œ“๊ธฐ)

 

2) ์ฝ”๋“œ์—์„œ ์ถ”๊ฐ€ํ•˜๊ธฐ

์ฝ”๋“œ์ƒ์—์„œ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Command +  ํด๋ฆญํ•˜๋ฉด ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

4. Asset ์ถ”๊ฐ€ํ•˜๊ธฐ

์™ผ์ชฝ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ์—์„œ Asset ์•„์ด์ฝ˜ ํด๋ฆญ ํ›„, ๊ธฐ์กด Asset ๋“ค์„ ์‚ญ์ œํ•œ ํ›„ I am Rich Asset ๋“ค์„ import ํ•ด์ค€๋‹ค.

 

5. ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ณ  ๋น„์œจ ๋งž์ถฐ์„œ ๋ฐฐ์น˜ํ•˜๊ธฐ

Image ์— resize, ratio ๋“ฑ์˜ ์†์„ฑ์„ ๋ถ€์—ฌํ•˜๊ณ  ์‹ถ์œผ๋ฉด Object ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Modifier๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack{
            Color(.systemTeal)
                .edgesIgnoringSafeArea(.all)
            VStack {
                Text("I Am Rich")
                    .font(.system(size:40))
                    .fontWeight(.bold)
                    .foregroundColor(Color.white)
                Image("diamond")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 200, height: 200, alignment: .center)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .previewDevice("iPhone 11")
    }
}

๋น„์œจ์ด ์•„๋‹Œ ์‚ฌ์ด์ฆˆ ์ž์ฒด๋ฅผ ์ง€์ •ํ•  ๋• frame() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์ž.

 

 

728x90