本文介绍了SwiftUI 表单未使用 Spacer() 正确定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个个人资料页面,其中底部有一个带有各种选项的表单.但是,当我将 Spacer() 插入到 VStack 中时,表单不会像它应该的那样移动到屏幕底部.我尝试用文本替换表单并且它工作正常,移动到屏幕底部.所以我假设它与表单有关.

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.

这是我的代码

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)
        }
    }
}

推荐答案

只要 Form 的底部是未知的(因为 Form 是可滚动的并且它的项目从顶部开始),swiftUI 不知道应该在它们之间放置多少空间这两种观点.所以 Spacer() 没有帮助.

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.

试试这个:

提供您的表单:例如,.frame(height: 500).padding(.top, 400) 修饰符.

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

这篇关于SwiftUI 表单未使用 Spacer() 正确定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:01