Skip to content

Latest commit

ย 

History

History
55 lines (45 loc) ยท 1.56 KB

File metadata and controls

55 lines (45 loc) ยท 1.56 KB

ZStack

๊ณต์‹๋ฌธ์„œ : A view that overlays its subviews, aligning them in both axes.

ํ•˜์œ„ ๋ทฐ๋ฅผ ๊ฒน์ณ์„œ ํ‘œ์‹œํ•˜๋ฉฐ, ํ•˜์œ„๋ทฐ๋“ค์„ ์ˆ˜ํ‰ ๋ฐ ์ˆ˜์ง์ถ•์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ทฐ์ด๋‹ค.

Declaration

@MainActor @frozen @preconcurrency
struct ZStack<Content> where Content : View

VStack, HStack๊ณผ ๊ฐ™์ด Viewํ˜•์‹์˜ ์ปจํ…ํŠธ๋ฅผ ์ œ๋„ค๋ฆญํ•˜๊ฒŒ ๋ฐ›๋Š”๋‹ค.

Overview

์ฃผ๋กœ ํ•œ ํ™”๋ฉด์—์„œ ๋ทฐ๋ฅผ ๊ฒน์ณ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

let colors: [Color] =
    [.red, .orange, .yellow, .green, .blue, .purple]


var body: some View {
    ZStack {
        ForEach(0..<colors.count) {
            Rectangle()
                .fill(colors[$0])
                .frame(width: 100, height: 100)
                .offset(x: CGFloat($0) * 10.0,
                        y: CGFloat($0) * 10.0)
        }
    }
}

ํ•ด๋‹น ์˜ˆ์‹œ์—์„œ๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ์‚ฌ๊ฐํ˜•์„ ๋ฌด์ง€๊ฐœ์ƒ‰ ์ˆœ์„œ๋กœ ์ฑ„์›Œ์„œ ZStack์— ์–น์–ด ๋‘์—ˆ๋‹ค.

์™„์ „ํžˆ ๊ฒน์น˜๋ฉด ์•Œ์•„ ๋ณผ ์ˆ˜ ์—†์œผ๋‹ˆ, offset์„ ์กฐ๊ธˆ ์”ฉ ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ์œผ๋ฉฐ, ํ•ด๋‹น ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋ฌด์ง€๊ฐœ์ƒ‰ ์ˆœ์„œ๋กœ ์Œ“์—ฌ์žˆ๋Š” ์‚ฌ๊ฐํ˜•์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

.zIndex()

zIndex ๋ชจ๋””ํŒŒ์ด์–ด๋ฅผ ํ†ตํ•ด์„œ ZStack์•ˆ์—์„œ ์˜ฌ๋ ค์ง€๋Š” ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

ZStack {
    Rectangle()
        .fll(Color.red)
        .frame(width: 100, height: 100)
        .zIndex(1)
    Rectangle()
        .fll(Color.green)
        .frame(width: 100, height: 100)
        .zIndex(1)
}