I’m trying to understand the expected behavior of TabView when using .tabViewStyle(.page) on iPadOS with a hardware keyboard.
When I place a TabView in page mode, swipe gestures correctly move between pages. However, left and right arrow keys do nothing by default, even when the view is made focusable. This feels a bit surprising, since paging with arrow keys seems like a natural keyboard interaction when a keyboard is attached.
Right now, to get arrow-key navigation working, I have to manually:
- Make the view focusable
- Listen for arrow key presses
- Update the selection state manually
This works, but it feels a little tedious for something that seems like it could be built-in.
import SwiftUI
struct PageTabsExample: View {
@State private var selection = 0
private let pageCount = 3
var body: some View {
TabView(selection: $selection) {
Color.red.tag(0)
Color.blue.tag(1)
Color.green.tag(2)
}
.tabViewStyle(.page)
.indexViewStyle(.page)
.focusable(true)
.onKeyPress(.leftArrow) {
guard selection > 0 else { return .ignored }
selection -= 1
return .handled
}
.onKeyPress(.rightArrow) {
guard selection < pageCount - 1 else { return .ignored }
selection += 1
return .handled
}
}
}
My questions:
- Is this lack of default keyboard paging for page-style TabView intentional on iPadOS with a hardware keyboard?
- Is there a built-in way to enable arrow-key navigation for page-style TabView, or is manual handling the expected approach?
- Does my approach above look like the “SwiftUI-correct” way to do this, or is there a better pattern for integrating keyboard navigation with paging?
- For this kind of behavior, is it generally recommended to use
.onKeyPresslike I’m doing here, or would.keyboardShortcutbe more appropriate (for example, wiring arrow keys to actions instead)?
Any guidance or clarification would be greatly appreciated. I just want to make sure I’m not missing a simpler or more idiomatic solution.
Thanks!