如何在SwiftUI中使视图具有最大宽度和填充?

3
我正在研究如何使SwiftUI的视图适应不同的屏幕尺寸。目前,我正在尝试创建一个注册屏幕,该屏幕应具有一定的最大宽度,但如果在小屏幕上无法应用最大宽度,则仍应具有填充。
我在根视图中使用GeometryReader来获取视图的宽度并将其应用于“注册”按钮。因此,我尝试为GeometryReader添加填充,但没有成功。原因是,您可以设置GeometryReader的maxWidth,但它会比屏幕尺寸更宽。
我的代码如下:
struct RegisterPage: View {
    @State private var email: String = ""
    @State private var username: String = ""
    @State private var password: String = ""

    var body: some View {
        GeometryReader { geometry in
            VStack {
                TextField("login.email_placeholder", text: self.$email)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.bottom)
                TextField("login.username_placeholder", text: self.$username)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.bottom)
                TextField("login.password_placeholder", text: self.$password)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.bottom)
                Button(
                    action: {},
                    label: {
                        Text("login.register_button")
                            .frame(width: geometry.size.width)
                            .padding()
                            .background(Color.blue)
                            .foregroundColor(Color.white)
                            .cornerRadius(5)
                    }
                )
            }
        }
        .frame(maxWidth: 500)
    }
}

enter image description here enter image description here

1个回答

3
如果我理解你想做什么的话,你可以在 GeometryReader 上使用 padding 修饰符:
struct RegisterPage: View {
    @State private var email: String = ""
    @State private var username: String = ""
    @State private var password: String = ""

    var body: some View {
        GeometryReader { geometry in
            VStack {
                TextField("login.email_placeholder", text: self.$email)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.bottom)
                TextField("login.username_placeholder", text: self.$username)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.bottom)
                TextField("login.password_placeholder", text: self.$password)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.bottom)
                Button(
                    action: {},
                    label: {
                        Text("login.register_button")
                            .frame(width: geometry.size.width)
                            .padding()
                            .background(Color.blue)
                            .foregroundColor(Color.white)
                            .cornerRadius(5)
                    }
                )
            }
        }
        .frame(maxWidth: 500)
        .padding(50)
    }
}

编辑:查看maxWidth = 10000的结果

在此输入图片描述


我尝试过这个。但实际问题是,如果设置的maxWidth比实际屏幕更宽,那么真实的宽度不会被设置为屏幕宽度。它仍然使用maxWidth,导致出现上面显示的溢出。 - G. Marc
@G.Marc 你确定吗?看看我在答案中的编辑。我将maxWidth设置为10000,现在一切都被正确地调整大小了(请确保使用最新的xCode Beta,即xCode Beta 5)。 - superpuccio
这正是我想要的!我稍后会再次测试。你们也在使用 beta 5 吗? - G. Marc
@G.Marc 是的,我正在使用xCode 11 beta 5和MacOS Catalina beta 5。 - superpuccio
1
谢谢@superpuccio,它真的有效。之前我不知道自己做错了什么... :-) - G. Marc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接