I have created a SwiftUI view with a ZStack containing a Text and a Rectangle. Note the different values in the position of each, yet they end up in approximately the same location in portrait orientation. In landscape orientation, the Text stays in the same position relative to the top left corner of the screen, yet the rectangle moves. When I make the position values the same, the Rectangle appears to be consistently above and to the left of the Text, yet by different amounts in landscape vs. portrait orientations. I observe this same behaviour both in the Simulator running iOS 14.4 (iPhone and iPad both), and also on a physical iPad running iOS 14.3.
My view:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack(alignment: .leading) {
Text("abc").position(x: 150, y: 150)
Rectangle().size(width: 50, height: 50).stroke().position(x: 320, y: 450)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
And the result:
Why is the position treated differently for Text and Rectangle elements in a View? And how can I position both in the same location?