A Text element inside a ZStack(alignment: .bottom) is displayed at the bottom of the container (as expected). Tho, if the same ZStack is inside a GeometryReader, the alignment does not behave the same anymore.
This code:
var body: some View {
ZStack(alignment: .bottom) {
VStack {
Text("Outside geo")
}
GeometryReader { geo in
ZStack(alignment: .bottom) {
VStack {
Text("Inside geo")
}
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
produces this result:
If I'd want the "inside geo" to be placed at the bottom too, I'd have to add alignment: .bottom inside its parent ZStack .frame(...) modifier.
What's the background for this behavior? Why can I use ZStack(alignment: ...) in one case, but have to use ZStack {...}.frame(alignment: ...) in the other to achieve the same result?


It might look strange, but the text "Inside geo" in the
GeometryReaderis correctly aligned at the bottom of theZStack- the problem is that yourZStackis not aligned at the bottom of theGeometryReader.The stacks occupy the minimum amount of space necessary, while the
GeometryReaderhas the opposite behaviour. So, theGeometryReaderis aligned at the bottom of your high-levelZStackbut it occupies the whole screen, while the inner-levelZStackis centred inside theGeometryReader.Try putting a
.background(someColor)on each view and you'll see where they are positioned.If you want the "Inside geo" to be at the bottom of the screen, you need to tell the
ZStackto get placed at the bottom with thealignmentparameter - like this: