Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI coordinate space

Tags:

swiftui

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".

like image 1000
inokey Avatar asked Jun 04 '19 12:06

inokey


2 Answers

.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")
        }
    }
like image 143
Cjay Avatar answered Oct 06 '22 23:10

Cjay


You can use 'padding' instead of position.

Text("Hello World").padding(EdgeInsets.init(top: 100, leading: 0, bottom: 0, trailing: 0))
like image 28
Markicevic Avatar answered Oct 07 '22 01:10

Markicevic