Home > Net >  How do I vertically center form view in swiftUI?
How do I vertically center form view in swiftUI?

Time:05-21

I am trying to use a form for a username/password login screen By default, the form aligns to the top of the screen, but I want it center aligned vertically and horizontally. Any ideas on how to do that?

struct LoginView: View {

@State var username: String = ""

@State var password: String = ""

var body: some View {
    VStack {
            Form {
                TextField("Username", text: $username)
                
                TextField("Password", text: $password)
            }
    }
    .frame(width: 400, height: 200, alignment: .center)
    
}
}

this is how it looks right now

I tried using a VStack and using a .frame modifier but I got a result that looked like this which is NOT what I want

This is with .frame and VStack

CodePudding user response:

    var body: some View {
        VStack(spacing: 0) {
            Color(uiColor: .systemGroupedBackground)
                List {
                    TextField("Username", text: $username)
                    TextField("Password", text: $password)
                }
            Color(uiColor: .systemGroupedBackground)
        }
        .ignoresSafeArea()
    }
  • Related