Swift UI 可以在 TextFields 和 Pickers 之间进行 Tab 键聚焦吗?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过在“系统偏好设置”>“使用键盘导航在控件之间移动焦点”启用“使用键盘导航在控件之间移动焦点”来实现这一点。键盘>快捷方式。
启用此设置后,我的选取器开始充当制表位。不幸的是,必须在每个用户的设备上启用它才能生效;但是,我没有看到任何解决办法。
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
在 macOS 14 上,该设置的位置和名称已更改:

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