I have a custom view:
struct ImageContent: View {
var body: some View {
Image("smile")
.resizable()
.scaledToFit()
}
}
Which is being placed into another view with a GeometryReader
:
var body: some View {
GeometryReader { geometry in
ImageContent()
//Image("smile").resizable().scaledToFit()
}
}
The problem is, the ImageContent
view is not centered on the screen, it is being placed on the top, however, by removing the ImageContent
subview and directly adding the view's content into the geometry reader will fix the issue (see picture).
Also, removing the GeometryReader
can fix the issue as well.
I need the subview because I will be implementing some additional logic, and also need the GeometryReader
because there is a gesture added to the Image
that uses it.
Any idea?
Try the following (built-in container by default expanded to size of GeometryReader and have explicit default alignment set to center by both dimensions). Tested with Xcode 11.2.
var body: some View {
GeometryReader { geometry in
VStack { // explicit container with center default alignment
ImageContent()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}