Center align form in swiftUI

Hi,

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? This is how it looks right now

I want this form center aligned vertically on the screen

Replies

You could:

  • embed the form in a VStack { }
  • set the frame of VStack:
   VStack {
        Form {
            TextField("UserName", text: $user)
            TextField("Password", text: $pswd)
       }
    }
    .frame(width: 400, height: 150, alignment: .center)  // adjust size as needed
  • This doesn't get the desired result :(

    It moves the vstack in the middle but then the background looks really weird as most of the screen is white and then the VStack is greyish.

    Plus, this makes the form not be center aligned

Add a Comment