SwiftUI 更改每个文本字段的键盘类型
我有多个 TextField,对于其中一个,我需要将 KeyboardType 切换为 .numberPad。显然,使用 .keyboardType(.numberPad) 修饰符确实很容易,但问题是,当默认键盘已经显示时,它不会关闭然后调出 numberPad。相反,它只是立即切换,这导致用户必须手动向上滑动才能看到他们将在其中输入数字的文本字段。
我需要做的是完全关闭默认键盘,然后显示数字键盘,以便将正确的文本字段推到屏幕上。
我已经尝试了 numberField TextField 的各种修饰符,例如:
.onAppear {
isFocused = nil
isFocused = .numberField
}
.onAppear {
isFocused = nil
sleep(1)
isFocused = .numberField
}
.focused($isFocused, equals: nil)
.keyboardType(.numberPad)
.focused($isFocused, equals: .numberField)
无济于事。键盘切换,但屏幕未滚动到正确的字段。
由于这是一个多字段表单,我使用提交按钮在字段之间切换,如下所示:
TextField("", text: $field1)
.focused($isFocused, equals: .field1)
.keyboardType(.default)
.onSubmit {
if isFocused == .field1 {
isFocused = .numberField
}
}
.submitLabel(.next)
TextField("", text: $numberField)
.focused($isFocused, equals: .numberField)
.keyboardType(.numberPad)
如果我手动关闭 field1 的默认键盘,然后点击 numberField TextField,则 numberPad 会出现并且字段正确聚焦。因此,我需要以某种方式自动关闭默认键盘,然后显示数字键盘,以便用户将正确的字段聚焦在屏幕上。
I have multiple TextFields and for one of them I need to switch the keyboardType to .numberPad. Obviously this is really easy using the .keyboardType(.numberPad) modifier, but the issue is that when the default keyboard is already shown it doesn't dismiss and then bring up the numberPad. Instead, it just switches immediately which causes the user to have to manually swipe up to see the TextField in which they will be entering numbers.
What I need to do is completely dismiss the default keyboard and then show the numberPad so that the correct TextField is pushed up on screen.
I've tried various modifiers for the numberField TextField such as:
.onAppear {
isFocused = nil
isFocused = .numberField
}
.onAppear {
isFocused = nil
sleep(1)
isFocused = .numberField
}
.focused($isFocused, equals: nil)
.keyboardType(.numberPad)
.focused($isFocused, equals: .numberField)
None work. The keyboard switches, but the screen does not scroll to the correct field.
As this is a multi-field form, I am using the submit button to switch between fields like this:
TextField("", text: $field1)
.focused($isFocused, equals: .field1)
.keyboardType(.default)
.onSubmit {
if isFocused == .field1 {
isFocused = .numberField
}
}
.submitLabel(.next)
TextField("", text: $numberField)
.focused($isFocused, equals: .numberField)
.keyboardType(.numberPad)
If I manually dismiss the default keyboard for field1 and then tap on the numberField TextField, the numberPad comes up and the field focuses correctly. So, I need to somehow automatically dismiss the default keyboard and then show the numberPad so the correct field is focused on screen for the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了关闭现有键盘然后显示新类型的键盘,同时保持键盘回避并专注于正确的 TextField,您需要使用 DispatchQueue,如下所示:
通过使用 DispatchQueue,您允许应用程序继续运行,同时完全关闭键盘,等待 x 秒(在我的示例中为 0.3 秒,感觉相当瞬时),然后调出新键盘。
In order to dismiss an existing keyboard and then show a new type of keyboard, while maintaining keyboard avoidance and focusing on the correct TextField, you need to use DispatchQueue as follows:
By using DispatchQueue you allow the app to keep running, while completely dismissing the keyboard, waiting x seconds (in my example it's 0.3 seconds which feels pretty instantaneous) and then bringing up the new keyboard.