SwiftUI 更改每个文本字段的键盘类型

发布于 2025-01-12 15:47:05 字数 1103 浏览 4 评论 0原文

我有多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2025-01-19 15:47:05

为了关闭现有键盘然后显示新类型的键盘,同时保持键盘回避并专注于正确的 TextField,您需要使用 DispatchQueue,如下所示:

TextField("", text: $field1)
    .focused($isFocused, equals: .field1)
    .keyboardType(.default)
    .onSubmit {
        if isFocused == .field1 {
            isFocused = nil
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                isFocused = .numberField
            }
        }
    }

通过使用 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:

TextField("", text: $field1)
    .focused($isFocused, equals: .field1)
    .keyboardType(.default)
    .onSubmit {
        if isFocused == .field1 {
            isFocused = nil
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                isFocused = .numberField
            }
        }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文