Modifiers
onTapGesture
Adds a tap gesture recognizer.
onTapGesture(handler)
| Parameter | Type | Description |
|---|---|---|
handler | () => void | Function to call when tapped. |
Adds a tap gesture recognizer.
Returns: ModifierConfig
See: Official SwiftUI documentation.
Works with
You can add onTapGesture to any view. It changes what you see on 27 of the 39 components.
Needs care
It works on these components, but not in the way that you may expect.
| Component | What to know |
|---|---|
| VStack | Touches on the empty space between children are not detected. Add contentShape('rectangle') before this modifier. |
| HStack | Touches on the empty space between children are not detected. Add contentShape('rectangle') before this modifier. |
| ZStack | Touches on the empty space between children are not detected. Add contentShape('rectangle') before this modifier. |
| LazyVStack | Touches on the empty space between children are not detected. Add contentShape('rectangle') before this modifier. |
| LazyHStack | Touches on the empty space between children are not detected. Add contentShape('rectangle') before this modifier. |
| ScrollView | Put the handler on a child view. A gesture on the scroll view itself can stop the scroll. |
No visible effect
These components accept it, but you see no change.
| Component | Why nothing happens |
|---|---|
| Spacer | In SDK 57, Spacer does not send modifier events to JavaScript, so the handler does not run. |
| Divider | In SDK 57, Divider does not send modifier events to JavaScript, so the handler does not run. |
| Button | Button handles taps itself. Use its onPress prop. A tap gesture on a button can stop the button action. |
| Toggle | Toggle handles taps itself. Use its onValueChange prop. |
| Checkbox | Checkbox handles taps itself. Use its onValueChange prop. |
| Slider | In SDK 57, Slider does not send modifier events to JavaScript, so the handler does not run. |
| Link | Link opens its URL itself. A tap gesture can stop the link. |
| Menu | In SDK 57, Menu does not send modifier events to JavaScript, so the handler does not run. |
| ContextMenu | In SDK 57, ContextMenu does not send modifier events to JavaScript, so the handler does not run. |
| ProgressView | In SDK 57, ProgressView does not send modifier events to JavaScript, so the handler does not run. |
| Gauge | In SDK 57, Gauge does not send modifier events to JavaScript, so the handler does not run. |
| AccessoryWidgetBackground | In SDK 57, AccessoryWidgetBackground does not send modifier events to JavaScript, so the handler does not run. |
Sources
The original Expo docs for this page:
- onTapGesture (Modifiers) (
source-of-truth/versions/v57.0.0/sdk/ui/swift-ui/modifiers.md)