Notice
Recent Posts
Recent Comments
Link
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[SwiftUI] VStack, HStack, ZStack ๋ณธ๋ฌธ

apple/๐Ÿš SwiftUI & Combine

[SwiftUI] VStack, HStack, ZStack

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2022. 5. 18. 15:34

VStack, HStack, ZStack

 

โœ… ์Šคํƒ๋ทฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

โœ… VStack

import SwiftUI

struct MyVstack : View {
    
    var body: some View {
    		// VStack์—๋Š” ๊ธฐ๋ณธ์ ์ธ spacing์ด ์žˆ์–ด์„œ 0์œผ๋กœ ํ•ด์ฃผ๊ธฐ 
            // alignment๋Š” ์ •๋ ฌ ์˜ต์…˜
            VStack(alignment: .trailing, spacing: 0) {
                        
                        // ์šฐ์ธก์œผ๋กœ ๋ถ™์ด๊ธฐ ์œ„ํ•œ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•!
                        // ๊ณต๊ฐ„์„ ์•„์˜ˆ ์ฃผ์–ด์„œ ์ •๋ ฌํ•˜๊ธฐ
            //            Spacer() 
                        
                        // opacity(0) ํ†ตํ•ด์„œ ์„ ์„ ์•ˆ๋ณด์ด๊ฒŒ ํ•œ๋‹ค.
                        Divider().opacity(0)
                        
                        // ์•„์˜ˆ Rectangle ํ•˜๋‚˜ ๋” ๋งŒ๋“ค๊ธฐ
            //            Rectangle()
            //                .frame(height: 1)
                        
                        // 
                        Text("๊ธ€์ž")
                            .font(.system(size: 30))
                            .fontWeight(.heavy)
            //                .padding(.bottom, 100) // ์•„๋ž˜์— ์—ฌ๋ฐฑ์ฃผ๊ธฐ
                        
                        Rectangle()
                            .frame(width: 100, height: 100)
                            .foregroundColor(Color.red)
            //                .padding(.vertical, 100) // ์ˆ˜์ง์œผ๋กœ ์—ฌ๋ฐฑ์ฃผ๊ธฐ 
                            
                        Rectangle()
                        .frame(width: 100, height: 100)
                        .foregroundColor(Color.yellow)
                        
                        // spacer๋ฅผ ํ™œ์šฉํ•ด์„œ ์›ํ•˜๋Š” ๋งŒํผ ํฌ๊ธฐ ์กฐ์ ˆ
                        Spacer()
                            .frame(height: 50)
                        
                        Rectangle()
                        .frame(width: 100, height: 100)
                        .foregroundColor(Color.blue)
                        
                        // ์œ„์— Spacer 1๊ฐœ ์•„๋ž˜์— 3๊ฐœ ๋‘์–ด์„œ ์œ„์•„๋ž˜๋กœ 1:3 ๋งŒํผ ๊ณต๊ฐ„์„ ํ™•๋ณดํ•จ
            //            Spacer()
            //            Spacer()
            //            Spacer()
                        
                    } // VStack
                    .frame(width: 300) // width๋งŒ 300 height๋Š” ์ž๋™์ง€์ •
                    .background(Color.green) 
            //        .edgesIgnoringSafeArea(.all)
                    
                
    }
}

VStack

โœ… HStack

import SwiftUI

struct MyHstack : View {
    var body: some View {
        
        HStack(alignment: .center) {
            
//            Divider()
            
//            Rectangle()
//                .frame(width: 100)
//            .foregroundColor(.red)
            
//            Rectangle()
//                .frame(width: 100, height: 100)
//                .foregroundColor(.red)
            
            Image(systemName: "flame.fill")
                .foregroundColor(.white)
                .font(.system(size: 70)) // systemImage์—์„œ ํฌ๊ธฐ ์กฐ์ ˆํ•˜๋Š” ๋ฐฉ๋ฒ•
            
            
            Rectangle()
            .frame(width: 100, height: 100)
            .foregroundColor(.yellow)
            
            Rectangle()
            .frame(width: 100, height: 100)
            .foregroundColor(.blue)
            
        }
        .padding()
        .background(Color.green)
        
    }
}

HStack

โœ… ZStack

zIndex๋Š” ๊ธฐ๋ณธ์ด 0์ด๋ฉฐ, 0์ธต๋ถ€ํ„ฐ ์ˆซ์ž๊ฐ€ ๋†’์•„์งˆ ์ˆ˜๋ก ์œ„์— ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ธฐ.

 

import SwiftUI

struct MyZstack : View {
    var body: some View {
        
        ZStack {
            
            Rectangle()
                .frame(width: 50, height: 50)
                .foregroundColor(Color.yellow)
                .zIndex(2) // zIndexํ†ตํ•ด์„œ index์กฐ์ •
                .offset(y: -100)
//                .padding(.bottom, 100)
            
            Rectangle()
                .frame(width: 100, height: 100)
                .foregroundColor(Color.red)
                .zIndex(1)
            
            Rectangle()
                .frame(width: 150, height: 150)
                .foregroundColor(Color.blue)
                .zIndex(0)
        }
        
    }
}

 

'apple > ๐Ÿš SwiftUI & Combine' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SwiftUI] NavigationView  (0) 2022.05.18
[SwiftUI] List (tableView in UIKit)  (0) 2022.05.18
[SwiftUI] Alert  (0) 2022.05.18
[SwiftUI] How to set Image in SwiftUI  (0) 2022.05.18
[SwiftUI] Info.plist๊ฐ€ ์—†์„ ๋•Œ๐Ÿค”  (0) 2022.02.23
Comments