制作 .toolbar 以将文本添加到 SwiftUI 中的 Textfield

问题描述 投票:0回答:1

我在 swiftUI 中有以下代码。

private var emailSection: some View {
        Section {
            HStack {
                UVFloatingTextField(title: "E-Mail", text: $email, keyboardType: .emailAddress)
                    .toolbar {
                        ToolbarItemGroup(placement: .keyboard) {
                            Button("@gmail.com") {
                                print("Clicked")
                            }
                            
                            Spacer()
                            
                            Button("Done") {
                                isInputActive = false
                            }
                        }
                    }

                if email.isEmpty {
                    MandatoryInfo()
                }
            }
        } header: {
            Text("Angaben zur E-Mail")
                .sectionHeader()
        }
    }

输出如下所示。这是我的文本字段

enter image description here

我想添加更多按钮,例如@gmail.com,当用户单击该按钮时,它将完成文本字段中的输入。

例如,用户在文本字段中键入“user123”,而不是手动写入@gmail.com,而是单击“@gmail.com”,将其写入文本字段。如果有更好的方法,然后使用按钮或任务栏,我也欢迎建议。谢谢!

我本来希望让按钮作为我自己的自动完成工具工作,但我不知道如何制作它。

swift swiftui autocomplete toolbar
1个回答
0
投票

我还没有测试过这个,但我很确定这应该有效

Button("@gmail.com") {
    print("Clicked")
    self.email += "@gmail.com"
}
© www.soinside.com 2019 - 2024. All rights reserved.