他们
decimalPad
键盘没有返回按钮。所以我喜欢添加一个使用键盘工具栏的工具。但如何创建提交按钮呢?
struct ContentView: View {
@State private var text: String = ""
var body: some View {
TextField("0", text: $text)
.keyboardType(.decimalPad)
.onSubmit {
// do something
}
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Submit Button") {
// trigger textfield onSubmit() manually
}
}
}
}
}
你可以尝试像这样简单的事情,使用
....endEditing(true)
:
struct ContentView: View {
@State private var text: String = ""
var body: some View {
TextField("0", text: $text).border(.red)
.keyboardType(.decimalPad)
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Submit Button") {
print("do something")
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.windows.first?.endEditing(true)
}
}
}
}
}