SwiftUI Form not positioning correctly using Spacer()

自作多情 提交于 2021-01-05 06:58:19

问题


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.

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@email.com")
                    .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)
        }
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/59633882/swiftui-form-not-positioning-correctly-using-spacer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!