ios - 使用 Swiftui 我们如何创建用户表单

标签 ios swift swiftui

我想创建一个表单,其中所有文本字段都需要从相同的对齐方式开始,如下图所示,它不是从相同的对齐方式开始的。名字、姓氏和电话号码所有文本字段未正确对齐在一条垂直线上。

VStack(){
        HStack(){
            Text("First Name")
            TextField("First name", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
        }
        HStack(){
            Text("Last Name")
            TextField("Last Name", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
        }
        HStack(){
            Text("Phone number")
            TextField("Phone number", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
        }
        Spacer()
    }.padding(10)

Like in iamge

最佳答案

        VStack{
            HStack(){
                Text("First Name").frame(width: 120, alignment: .leading)
                TextField("First name", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
            }
            HStack(){
                Text("Last Name").frame(width: 120, alignment: .leading)
                TextField("Last Name", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
            }
            HStack{
                Text("Phone number").frame(width: 120, alignment: .leading)
                TextField("Phone number", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
            }
            Spacer()
        }.padding(10)

或更灵活的解决方案

struct TestView: View {
    @State var name = ""
    @State var width: CGFloat = 0

    struct Label: ViewModifier {
        @Binding var width: CGFloat

        func getWidth(content: Content, proxy: GeometryProxy) -> some View {
            if proxy.size.width > width {
                RunLoop.main.perform {
                    self.width = proxy.size.width
                }
            }
            return content
        }

        func body(content: Content) -> some View {
            content.overlay(GeometryReader { proxy in
                self.getWidth(content: content, proxy: proxy)
            }).frame(minWidth: self.width, alignment: .leading)
        }
    }

    var body: some View {
        VStack(alignment: .center){
            HStack(){
                Text("First Name").modifier(Label(width: $width))
                TextField("First name", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
            }
            HStack(){
                Text("Last Name").modifier(Label(width: $width))
                TextField("Last Name", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
            }
            HStack{
                Text("Phone numbe").modifier(Label(width: $width))
                TextField("Phone number", text: $name).textFieldStyle(RoundedBorderTextFieldStyle())
            }

            Spacer()
        }.padding(10)
    }
}

关于ios - 使用 Swiftui 我们如何创建用户表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58057022/

相关文章:

iOS:无法在 didFinishLaunchingWithOptions 中将 UIApplicationidleTimerDisabled 设置为 YES

ios - 显示 JSON 数据的 UITableView 中的响应滞后

swift - 如何使用绑定(bind)关联 Swift 枚举?

swiftui - 如何在 SwiftUI 或无限 ListView 中实现列表分页?

ios - 在 GKMatchmakerviewcontroller 中处理取消

ios - 具有自动布局的 UIScrollView 内的动态 UILabel 高度

ios - 如何用另一个 NSTimer 来停止 NSTimer?

swift - 如何从 WatchOS 中的 SwiftUI 列表中删除分隔符?

ios - 如何使用 IAP 在不同国家以适当的货币显示价格

ios - Swift UI 中带有圆角半径的按钮边框