Language/Swift

SwiftUI를 이용한 뷰를 겹겹이 쌓기 (ZStack)

IFLA 2022. 12. 8. 10:00

VStack과 HStack은 세로나 가로로 뷰를 쌓을 때 이용한다. 간혹 뷰를 겹쳐서 쌓아야 할 때 ZStack 뷰를 이용해 뷰들을 겹겹이 쌓을 수 있다.

첫 번째에 입력한 내용이 먼저 그려진 다음 후속 뷰가 그 위에 계층화된다. 앱이 실행 되는 동안 어떠한 뷰를 다른 뷰 뒤로 밀거나 탭할 때 특정 뷰를 앞으로 가져올 때 .zIndex() 수정자를 이용한다.

 

기본 코드

struct ContentView: View {
    var body: some View {
        ZStack {
            Text("1")
                .background(Color.yellow)
                .foregroundColor(.red)
                .zIndex(1)

            Text("Photo credit: 김테스트")
                .padding(4)
                .background(Color.black)
                .foregroundColor(.white)
                .offset(x: -10, y: 15)
        }
        .padding()
    }
}

 

실행 화면

 


개발자 Document

Apple Developer Documentation

 

Apple Developer Documentation

 

developer.apple.com