Search code examples
swiftalignmentswiftui

SwiftUI Alignment Bottom


I have the following code:

struct CircleView: View {
    var body: some View {
        ZStack(alignment: .bottom) {
            VStack{
                Spacer()
                Circle().frame(width: UIScreen.main.bounds.width/5)
            }
        }
    }
}

enter image description here

Why is it not aligning the circle on the bottom? I have a spacer which should take up all free space in the ZStack?

Thanks in advance.


Solution

  • Because Circle is a Shape and does not have own content size, so consumes all provided. You limited width, but not height, so Circle consumed all height (blue rect is all circle, but drown only where fit).

    So if you want to make it aligned, you have to limit it completely, like below

    enter image description here

    ZStack(alignment: .bottom) {
        VStack{
            Spacer()
            Circle()
                .frame(width: UIScreen.main.bounds.width/5, height: UIScreen.main.bounds.width/5)
        }
    }