Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Form not positioning correctly using Spacer()

I am trying to create a profile page, in which there is a form at the bottom with various options. However when I insert a Spacer() into the VStack, the form does not move to the bottom of the screen, as it should do. I tried replacing the Form with a Text and it worked fine, moving to the bottom of the screen. So I'm assuming it has something to do with the form.

the current layout

Here is my code

struct Profile: View {

    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    var body: some View {

        NavigationView {
            VStack {
                Image(systemName: "person.crop.circle")
                    .resizable()
                    .frame(width: 50, height: 50)
//                    .padding(.top)

                Text("[email protected]")
                    .font(.title)

                Spacer()

                Form {
                    Section {
                        //menuListItem(image: "gear", label: "Settings")
                        menuListItem(image: "questionmark.circle", label: "Help")                     menuListItem(image: "info.circle", label: "About")
                    }

                    Section {
                        HStack {
                            Spacer()
                            Button(action: {
                                UserDefaults.standard.set(false, forKey: "LoggedIn")
                                UserDefaults.standard.set(nil, forKey: "user_id")
                                UserDefaults.standard.set(nil, forKey: "school_id")
                                self.mode.wrappedValue.dismiss()
                            }) {
                                Text("Log Out")
                                    .font(.body)
                                    .foregroundColor(.red)
                            }
                            Spacer()
                        }
                    }
                }  
            }
            .navigationBarTitle("Profile", displayMode: .inline)
        }
    }
}

struct menuListItem: View {

    var image: String
    var label: String

    var body: some View {
        HStack {
            Image(systemName: image)
            Text(label)
                .font(.body)
        }
    }
}
like image 429
Arían Taherzadeh Avatar asked Feb 25 '26 03:02

Arían Taherzadeh


1 Answers

As long as bottom of the Form is unknown(because Form is scrollable and its items start from the top), swiftUI does not know how much space should put between those two views. So Spacer() does not help.

Try this:

Give your form: for example, .frame(height: 500) or .padding(.top, 400) modifier.

like image 56
FRIDDAY Avatar answered Feb 26 '26 17:02

FRIDDAY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!