peter flanagan
peter flanagan

Reputation: 9830

Top alignment/position of text in SwiftUI

I am using SwiftUI and want to position a Text element at the top of the screen.

My code looks like as follows and the output is below. As you can see it is in the middle of the screen. Can anyone advise how to update the position? I have tried to add alignment properties with little or no success.

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello World!")
    }
}

enter image description here

Upvotes: 1

Views: 1124

Answers (2)

pawello2222
pawello2222

Reputation: 54641

I have tried to add alignment properties with little or no success.

Alternatively, you can use frame with the .top alignment:

struct ContentView: View {
    var body: some View {
        Text("Hello World!")
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
    }
}

Upvotes: 2

AJ Aguasin
AJ Aguasin

Reputation: 96

You can use a Spacer() after the Text View to push it upwards.

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World!")
            Spacer()
        }
    }
}

Upvotes: 4

Related Questions