Keyboard order - SwiftUI
In SwiftUI, you can use the accessibilityRespondsToUserInteraction
view modifier to optimize keyboard navigation. By setting the property to false
, the element will be skipped with keyboard navigation. Other assistive technologies, such as VoiceOver can still focus on the element. This way you can provide screen reader users with alternative text for images, but skip focus for keyboard users. When a hardware keyboard is connected and VoiceOver is enabled, the image will be focusable.
For more precise control over the keyboard order, you can use accessibilitySortPriority
view modifier to specify the priority of the item when using Full Keyboard Access
.
VStack {
HStack {
Text("Top Left")
// Focused first
.accessibilitySortPriority(4)
// Allow interaction using Full Keyboard Access
.accessibilityRespondsToUserInteraction(true)
Text("Top Right")
// Focused third
.accessibilitySortPriority(2)
.accessibilityRespondsToUserInteraction(true)
}
HStack {
Text("Bottom Left")
// Focused second
.accessibilitySortPriority(3)
.accessibilityRespondsToUserInteraction(true)
Text("Bottom Right")
// Not focusable by Full Keyboard Access
.accessibilityRespondsToUserInteraction(false)
}
}