如何在 swiftUI iOS 14 中向数字键盘添加“完成”按钮

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

我需要创建在我们的应用程序中提交的电话号码文本,要输入我选择的数字键盘键盘的电话号码,在数字键盘中没有返回按钮来关闭键盘,如果有人知道请帮助我创建键盘工具栏中的完成按钮可关闭键盘。

应用程序最低支持版本是iOS14

提前致谢。

                        // MobileNumber textfield
                        Label {
                            CustomTextfield(placeholder: Text("Mobile Number"), fontName: "Avenir", fontSize: 14, fontColor: Color.white,foregroundColor: Color.white, text: $viewModel.mobileNumber)
                                .keyboardType(.numberPad)
                                .onChange(of: viewModel.mobileNumber) { newValue in
                                    if newValue.count > 10{
                                        viewModel.mobileNumber = String(newValue.prefix(10))
                                    }
                                }
                        } icon: {
                            Image("ic_Mobile")
                                .frame(width: 14, height: 14)
                                .padding(.leading)
                        }.modifier(AuthenticationTFModifier())

this is the mobile number textField code

ios swiftui keyboard textfield ios14
1个回答
0
投票

您可以使用

FocusState
toolbar
尝试这种方法 将
Done
按钮添加到数字键盘。

struct ContentView: View {
    @State private var text = ""
    @FocusState var keyboardDismiss: Bool
    
    var body: some View {
        TextField("number", text: $text)
            .keyboardType(.numberPad)
            .border(.red)
            .focused($keyboardDismiss)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") { keyboardDismiss = false }
                }
            }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.