1
votes

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
Just to understand what you want, think in terms of X, Y, and Z - a common coding scenario. X is horizontal, Y is vertical, and Z? Probably best described as layers. With this in mind are you wanting to position your image on top (or above) of the the other elements in the body, or on thetop of the other elements? If it's the former, we probably need to know what else is in the body. - dfd

2 Answers

1
votes

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)
        }
    }

0
votes

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.