我需要创建在我们的应用程序中提交的电话号码文本,要输入我选择的数字键盘键盘的电话号码,在数字键盘中没有返回按钮来关闭键盘,如果有人知道请帮助我创建键盘工具栏中的完成按钮可关闭键盘。
应用程序最低支持版本是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
您可以使用
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 }
}
}
}
}