swiftuiswiftui-zstack

Positioning inside ZStack


I want this red rectangle to be repositioned to the left bottom corner but for some reason my alignments and paddings do not work as I expected. What am i doing wrong and how to move it to the left bottom corner?

Code:

struct ContentView: View {
    
    var body: some View {

        ZStack {
                VStack(spacing: 0) {
                    Rectangle()
                        .fill(Color.red)
                        .frame(width: 197, height: 163)
                }
                .frame(width: 284, height: 269)
                .padding(.leading, 0)
                .padding(.bottom, 0)
                .background(Color.blue)
            }
    }
}

Solution

  • ZStack has a parameter alignment: Alignment which is an alignment in both axes.

      struct ContentView: View {
            
            var body: some View {
                
                ZStack(alignment:.bottomLeading) {
                    Rectangle()
                        .fill(Color.blue)
                        .frame(width: 284, height: 269)
                    Rectangle()
                        .fill(Color.red)
                        .frame(width: 197, height: 163)
                }
                
                
            }
        }