Swift UI 可以在 TextFields 和 Pickers 之间进行 Tab 键聚焦吗?

发布于 2025-01-11 05:20:32 字数 635 浏览 4 评论 0原文

我有一个 Swift UI 表单(在 Mac OS,Xcode 13 上),它有一些 TextFields,中间有一个带有两个文本选项的选择器。目前,用户可以按 Tab 键在文本字段之间移动焦点,但它会跳过选择器选项。我尝试将 .focusable() 添加到选择器和选择器内的文本选项,但没有效果。我希望用户能够按 Tab 键选择选取器组,并使用箭头键在选项之间移动,然后再次按 Tab 键移动到下一个 TextField。

这是我的代码的一个快速示例(缩写为相关内容)

Form
{
    TextField("First text field", text: $first)
        .focused($focused, equals: .first)

    Picker("Picker", selection: $pickerSelection)
    {
        Text("Option 1").tag(true as Bool?)
        Text("Option 2").tag(false as Bool?)
    }

    TextField("Second text field", text: $second)
        .focused($focused, equals: .second)
}

I have a Swift UI Form (on Mac OS, Xcode 13), and it has some TextFields, and in the middle a Picker with two Text options. Currently, the user can press the tab key to move focus between the TextFields just fine, but it skips over the picker options. I've tried adding .focusable() to the Picker and to the Text options inside the picker to no effect. I would like for the user to be able to press tab to select the Picker group, and use the arrow keys to move between the options, and press tab again to move onto the next TextField.

Here's a quick example of what my code looks like (shortened to what's relevant)

Form
{
    TextField("First text field", text: $first)
        .focused($focused, equals: .first)

    Picker("Picker", selection: $pickerSelection)
    {
        Text("Option 1").tag(true as Bool?)
        Text("Option 2").tag(false as Bool?)
    }

    TextField("Second text field", text: $second)
        .focused($focused, equals: .second)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

撩动你心 2025-01-18 05:20:32

我通过在“系统偏好设置”>“使用键盘导航在控件之间移动焦点”启用“使用键盘导航在控件之间移动焦点”来实现这一点。键盘>快捷方式。

启用此设置后,我的选取器开始充当制表位。不幸的是,必须在每个用户的设备上启用它才能生效;但是,我没有看到任何解决办法。

Mac 设置

I was able to achieve this by enabling "Use keyboard navigation to move focus between controls" under System Preferences > Keyboard > Shortcuts.

Once this setting was enabled my Picker began acting as a tab stop. The unfortunate part of this is that it would have to be enabled on every user's device for it to take effect; however, I don't see any way around it.

Mac Settings

水中月 2025-01-18 05:20:32

在 macOS 14 上,该设置的位置和名称已更改:
macOS 系统设置

On macOS 14, the setting's location and name changed:
macOS System Settings

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