SwiftUI Login Page Layout

Hi Im trying to achieve this result in the image of this Stack overflow question

https://stackoverflow.com/questions/56623310/swiftui-login-page-layout


As you can see I already reached this point but I don't like my implementation

struct ContentView : View {
    @State var username: String = ""
    var body: some View {
        VStack(alignment: .leading) {
            Text("Login")
                .font(.title)
                .multilineTextAlignment(.center)
                .lineLimit(nil)
                Text("Please")
                    .font(.subheadline)
           
            HStack {
                VStack (alignment: .leading, spacing: 20) {
                    Text("Username: ")
                    Text("Password: ")

                }
                VStack {
                    TextField($username, placeholder: Text("type something here..."))
                    .textFieldStyle(.roundedBorder)
                    TextField($username, placeholder: Text("type something here..."))
                        .textFieldStyle(.roundedBorder)
                }
            }
        }.padding()
    }
}


Because in order to make the username and password text aligned exactly in the middle of the textfield, I had to put literal spacing value of

20
in the
VStack
which I don't like because most probably It won't work on different device sizes.

Anyone sees a better way to achieve the same result?

Thanks

Replies

As this is my first post. Not sure how to make this nice code view that you showed yet. I think this looks a little nicer and you don't need the spacing.


struct ContentView : View {

@State var username: String = ""

var body: some View {

VStack(alignment: .leading) {

Text("Login")

.font(.title)

.multilineTextAlignment(.center)

.lineLimit(nil)

Text("Please")

.font(.subheadline)

HStack {

Text("Username: ")

TextField($username, placeholder: Text("type something here..."))

.textFieldStyle(.roundedBorder)

}

HStack {

Text("Password: ")

TextField($username, placeholder: Text("type something here..."))

.textFieldStyle(.roundedBorder)

}

}.padding()

}

}