How to set alignment in main body view?

Viewed 2395

I am trying to align an Image in the first body view to the top but can not find a way to do this. I want set an alignment to .top like you would do in a ZStack, like this:

ZStack(alignment: .top) {
    Image("imagename")
    Text("Test")
}

In this code the text, in front of the image, would be at the top. How would I do the same but the image is the body view and the text is the image.

2 Answers

Another use for GeometryReader (read more here)!

    var body: some View {
        GeometryReader { geometry in
            VStack(alignment: .center) {
                Image(systemName: "person")
                }.frame(width: geometry.size.width, alignment: .top)
        }
    }

Another alternative is to use Spacer.

var body: some View {

  VStack(alignment: .center) {
      Image(systemName: "person")
      Spacer()   
  }

}

I actually wrote a blog post about how you can use Spacer to create fairly complex layouts.

Related