The problem
A lot of iPads now live in a Magic Keyboard case, and since iPadOS 26 every app has a real menu bar at the top of the screen. People who work that way expect Command-N to make something new, Command-F to search and the arrow keys to move through a list. They expect the pointer to show what is clickable. An app that only responds to touch feels broken in that setup. SwiftUI covers most of this with a few modifiers, but they interact in ways the documentation spreads across many pages. This article puts them in one place: shortcuts, the menu bar, focus-driven commands, keyboard navigation, and the pointer.
Start with commands, not loose shortcuts
There are two places a shortcut can live. You can attach .keyboardShortcut to a button somewhere in a view, or you can declare the action as a command on the scene. On iPad, prefer the second.
Apple’s article Building and customizing the menu bar with SwiftUI explains why. Commands go into the menu bar on both iPadOS and macOS, with the shortcut printed next to them. SwiftUI creates key commands for every command that has a shortcut, and those appear in the overlay people get by holding Command. Majid Jabrayilov described that overlay back in Commands in SwiftUI, when it was the only place iPad commands showed up. In iPadOS 26 the same declarations also fill the menu bar.
commands(content:), CommandMenu and CommandGroup are available from iOS 14.
import SwiftUI
@main
struct JournalApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
SidebarCommands()
EntryCommands()
}
}
}
SidebarCommands adds the standard Show/Hide Sidebar item to the View menu. Include it whenever the scene has a NavigationSplitView.
Where custom commands go
Apple’s menu bar article lists the three ways to place an item:
CommandMenu("Entry")creates a new top-level menu. The system inserts custom menus after View.CommandGroup(after: .newItem)orCommandGroup(before: ...)adds items next to a system group such as New.CommandGroup(replacing: .help)swaps out a system group.
The HIG page on the menu bar and the WWDC25 session Elevate the design of your iPad app agree on a few rules:
- Keep the system menus and their order. The system handles many standard items for you, such as Copy when text is selected.
- Order custom menu items by how often they are used, not alphabetically. Group related actions and push secondary ones into submenus.
- Give items SF Symbols, the same symbols the system uses for the same actions.
- Put each tab in the View menu with a shortcut if your app uses tab navigation.
- Never hide an item because it does not apply right now. Disable it so the menu stays predictable.
- Rows are taller on iPad than on Mac, so use submenus more readily.
The HIG adds one iPad-only rule: the menu bar is usually hidden in full-screen apps, so every command must also be reachable through the regular interface. The menu bar is an accelerator, not the only path to a feature.
Make commands follow the focused window
A command like “Delete Entry” needs to know which entry, in which window. With multiple windows on iPad, a global selection is wrong as soon as a second window opens. The answer is focused values. The window that has focus publishes its state, and the command reads it.
import SwiftUI
@Observable
final class JournalModel {
var entries: [Entry] = []
var selection: Entry.ID?
func addEntry() {
let entry = Entry(id: UUID(), title: "Untitled")
entries.insert(entry, at: 0)
selection = entry.id
}
func deleteSelected() {
entries.removeAll { $0.id == selection }
selection = nil
}
}
struct Entry: Identifiable, Hashable {
let id: UUID
var title: String
}
struct ContentView: View {
@State private var model = JournalModel()
var body: some View {
NavigationSplitView {
List(model.entries, selection: $model.selection) { entry in
Text(entry.title)
}
.navigationTitle("Journal")
} detail: {
Text(model.selection == nil ? "No entry selected" : "Entry")
}
.focusedSceneValue(model)
}
}
struct EntryCommands: Commands {
@FocusedValue(JournalModel.self) private var model
var body: some Commands {
CommandGroup(after: .newItem) {
Button("New Entry", systemImage: "square.and.pencil") {
model?.addEntry()
}
.keyboardShortcut("n")
.disabled(model == nil)
}
CommandMenu("Entry") {
Button("Delete Entry", systemImage: "trash", role: .destructive) {
model?.deleteSelected()
}
.keyboardShortcut(.delete, modifiers: .command)
.disabled(model?.selection == nil)
}
}
}
Passing an @Observable object straight to focusedSceneValue(_:) and reading it with @FocusedValue(JournalModel.self) uses the iOS 17 forms of those APIs, as Apple’s menu bar article shows. For value types, declare an entry with extension FocusedValues { @Entry var selectedEntryID: Entry.ID? } and publish it with the key-path form. Custom entries are always optional because nothing may be focused.
Choose between the two modifiers on purpose. focusedSceneValue publishes for the whole active window. focusedValue publishes only while focus is inside the modified view, so a “Delete” command bound to it goes dim when the sidebar has focus. Apple’s article uses exactly that example: a selection published from the detail list reads nil while the sidebar has focus.
Before iOS 17, the focused value APIs needed a custom FocusedValueKey. If you still support iOS 16, that is the pattern you will see in older posts.
Keyboard shortcuts in detail
keyboardShortcut(_:modifiers:) (iOS 14) takes a KeyEquivalent and an EventModifiers set. Paul Hudson’s quick-start entry and Majid’s Keyboard shortcuts in SwiftUI cover the basics well:
- Without
modifiers:, Command is assumed..keyboardShortcut("n")is Command-N. KeyEquivalenthas constants for keys you cannot type in a string literal:.return,.escape,.delete,.upArrowand friends.KeyboardShortcut.defaultActionbinds Return and.cancelActionbinds Escape. Use them on confirm and cancel buttons in sheets so they behave like system alerts.- Applied to a container, the shortcut goes to the first interactive child.
Majid also points out that the Simulator needs its keyboard capture turned on before shortcuts reach the app. That is the usual reason a shortcut “doesn’t work” during development.
The trap nobody expects is scope. Keith Harrison tested it in SwiftUI Keyboard Shortcut Scope: a shortcut stays live as long as its control is anywhere in the view hierarchy of the active window, even offscreen. A button on a root view still fires after you push a detail screen over it. His fix is to disable the control while it should not respond. That is another reason to prefer commands with focused values. Their enabled state follows focus by design.
Apple’s menu bar article also warns that the system reserves many shortcuts your app cannot override. Use the standard ones for their standard meanings (Copy, Paste, Undo, Find, Print). Invent new ones only for actions people use often. Check the HIG keyboard list before claiming Command plus a letter.
Keyboard navigation and focus
Shortcuts trigger actions. Focus lets people move around with Tab and the arrow keys. A List with a selection binding already supports arrow keys on a hardware keyboard. Forms and custom controls need some help.
@FocusState (iOS 15) is the tool. Majid’s Mastering FocusState recommends an optional Hashable enum for forms with several fields. Bind each field with focused(_:equals:). Set the state to move focus and set it to nil to dismiss the keyboard. defaultFocus(_:_:priority:) (iOS 17) picks the field that gets focus first.
For custom views, iOS 17 adds focusable(_:interactions:) and onKeyPress:
import SwiftUI
struct RatingControl: View {
@Binding var rating: Int
@FocusState private var isFocused: Bool
var body: some View {
HStack(spacing: 4) {
ForEach(1...5, id: \.self) { star in
Image(systemName: star <= rating ? "star.fill" : "star")
.onTapGesture { rating = star }
}
}
.padding(8)
.focusable(interactions: .edit)
.focused($isFocused)
.onKeyPress(.leftArrow) {
rating = max(1, rating - 1)
return .handled
}
.onKeyPress(.rightArrow) {
rating = min(5, rating + 1)
return .handled
}
.accessibilityElement()
.accessibilityLabel("Rating")
.accessibilityValue("\(rating) of 5")
.accessibilityAdjustableAction { direction in
switch direction {
case .increment: rating = min(5, rating + 1)
case .decrement: rating = max(1, rating - 1)
@unknown default: break
}
}
}
}
Return .ignored from onKeyPress for keys you do not handle so they continue up the chain. The accessibility lines are there because a control a keyboard user can adjust should be adjustable for VoiceOver users too, and it costs four lines.
The pointer
iPadOS 26 changed the pointer. Session 208 describes a new shape that tracks the input one to one, without snapping onto targets the way the iPadOS 13 pointer did. Hover feedback is now a Liquid Glass highlight that sits over the hovered control instead of the pointer morphing into it. System controls get this for free. Your job is custom controls and big tappable areas.
hoverEffect(_:) has been in SwiftUI since iOS 13.4, with .automatic, .highlight and .lift. Majid’s Hover effect in SwiftUI adds two tips that still hold. defaultHoverEffect(_:) (iOS 17) applies a style to a whole hierarchy so you cannot forget one view, and hoverEffectDisabled(_:) opts a view out.
import SwiftUI
struct PaletteSwatch: View {
let color: Color
let action: () -> Void
var body: some View {
Button(action: action) {
Circle()
.fill(color)
.frame(width: 32, height: 32)
.frame(width: 44, height: 44)
.contentShape(.hoverEffect, .circle)
.contentShape(.interaction, .circle)
}
.buttonStyle(.plain)
.hoverEffect(.highlight)
.accessibilityLabel("Color")
}
}
contentShape(.hoverEffect, ...) (iOS 15) tells the system the shape of the highlight, so a circular swatch does not get a square platter. The outer 44-point frame keeps the hit target at the HIG minimum while the visible circle stays small.
For hover-driven UI, such as showing actions when the pointer is over a row, use onHover(perform:) (iOS 13.4) or onContinuousHover(coordinateSpace:perform:) (iOS 17), which reports the location. Keep anything revealed on hover reachable by touch as well. Many iPads never see a pointer.
Two pointer APIs look tempting and are not there. pointerStyle(_:) is macOS 15 and visionOS 2 only. The custom hover effect group APIs are visionOS only, even though CustomHoverEffect itself lists iOS 18. If you need a custom pointer shape on iPad, drop to UIKit: UIPointerInteraction (iOS 13.4) in a UIViewRepresentable.
Traps
- Hiding instead of disabling. A menu that changes shape every time you open it teaches people nothing. Disable unavailable items.
- Shortcuts on views that never leave the hierarchy. See Keith Harrison’s test above. A shortcut on a tab you are not showing may still fire.
- Duplicate shortcuts. If a command and a view both claim Command-N, which one fires depends on where focus is and is hard to reason about. Give every shortcut one owner, preferably the command.
- Shortcuts that clash with the system. A shortcut the system reserves never reaches you. Test each new one with the Command overlay open.
- No keyboard, no menu. The HIG calls out that dynamic menu items only exist with a hardware keyboard. Everything must also be reachable by touch.
Checklist
| Task | API | Minimum OS |
|---|---|---|
| Add app actions to the menu bar | .commands, CommandMenu, CommandGroup |
iOS 14 |
| Standard sidebar item | SidebarCommands() |
iOS 14 |
| Shortcut on a command | keyboardShortcut(_:modifiers:) |
iOS 14 |
| Return and Escape in sheets | .keyboardShortcut(.defaultAction) / .cancelAction |
iOS 14 |
| Command state from the focused window | focusedSceneValue(_:), @FocusedValue(Type.self) |
iOS 17 |
| Command state from the focused view | focusedValue(_:_:) |
iOS 17 |
| Field focus in forms | @FocusState, focused(_:equals:) |
iOS 15 |
| Keyboard on custom controls | focusable(_:interactions:), onKeyPress |
iOS 17 |
| Pointer feedback | hoverEffect(_:), contentShape(.hoverEffect, _:) |
iOS 13.4 / 15 |
| One style for a whole hierarchy | defaultHoverEffect(_:) |
iOS 17 |
Hold Command in your app on an iPad with a keyboard and read the overlay. If it is empty, or lists items that make no sense in the current window, fix that first.
Sources and further reading
- Building and customizing the menu bar with SwiftUI Apple Developer Documentation
- The menu bar Apple Human Interface Guidelines
- Elevate the design of your iPad app (WWDC25) Apple, WWDC25
- Commands in SwiftUI Majid Jabrayilov, Swift with Majid
- Keyboard shortcuts in SwiftUI Majid Jabrayilov, Swift with Majid
- Hover effect in SwiftUI Majid Jabrayilov, Swift with Majid
- SwiftUI Keyboard Shortcut Scope Keith Harrison, Use Your Loaf
- How to add keyboard shortcuts using keyboardShortcut() Paul Hudson, Hacking with Swift
- Mastering FocusState property wrapper in SwiftUI Majid Jabrayilov, Swift with Majid