Does anyone know how does SwiftUI render in terms of coordinate space? It doesn't look like the frame's origin is now at the 0, 0
in the left top corner. For example adding a Text
with a modifier would offset the label outside of a view.
var body: some View {
Text("my long enough string")
.position(CGPoint(x: 0, y: 100))
}
The result is I can only see "ugh string".
.position(x, y) will position the center of the view to the specified coordinate with the parent.
You will need to use a Stacks/Container to position the displayed elements relative to each other using aligments and spacer.
Views always start out at the center of the screen.
Something like this:
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text("top left")
Spacer()
Text("bottom left")
}
Spacer()
VStack(alignment: .leading) {
Text("top right")
Spacer()
Text("bottom right")
}
}
You can use 'padding' instead of position.
Text("Hello World").padding(EdgeInsets.init(top: 100, leading: 0, bottom: 0, trailing: 0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With