Declaration
Initializers
6 SwiftUI initializers back <Stepper>. The props you pass pick one.
<Stepper
value: Binding<any>
step?: number
onEditingChanged?: (arg0: boolean) => void
>
children?: ReactNode
</Stepper>@export(implementation) nonisolated init<V>(
value: Binding<V>,
step: V.Stride = 1,
@ContentBuilder label: () -> Label,
onEditingChanged: @escaping (Bool) -> Void = { _ in }
)
where V : Strideablevalue*Binding<any>The
Bindingto a value that you provide.stepnumberThe amount to increment or decrement
valueeach time the user clicks or taps the stepper's increment or decrement buttons. Defaults to1.Default
`1`childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
label: () -> LabelonEditingChanged(arg0: boolean) => voidA closure that's called when editing begins and ends. For example, on iOS, the user may touch and hold the increment or decrement buttons on a stepper which causes the execution of the
onEditingChangedclosure at the start and end of the gesture.Default
`{ _ in }`
Also takes the common view props: modifiers, style and testID.
Overview
Use a stepper control when you want the user to have granular control while incrementing or decrementing a value. For example, you can use a stepper to:
- Change a value up or down by
1. - Operate strictly over a prescribed range.
- Step by specific amounts over a stepper's range of possible values.
The example below uses an array that holds a number of Color values,
a local state variable, value, to set the control's background
color, and title label. When the user clicks or taps the stepper's
increment or decrement buttons, SwiftUI executes the relevant
closure that updates value, wrapping the value to prevent overflow.
SwiftUI then re-renders the view, updating the text and background
color to match the current index:
struct StepperView: View {
@State private var value = 0
let colors: [Color] = [.orange, .red, .gray, .blue,
.green, .purple, .pink]
func incrementStep() {
value += 1
if value >= colors.count { value = 0 }
}
func decrementStep() {
value -= 1
if value < 0 { value = colors.count - 1 }
}
var body: some View {
Stepper {
Text("Value: \(value) Color: \(colors[value].description)")
} onIncrement: {
incrementStep()
} onDecrement: {
decrementStep()
}
.padding(5)
.background(colors[value])
}
}The following example shows a stepper that displays the effect of
incrementing or decrementing a value with the step size of step with
the bounds defined by range:
struct StepperView: View {
@State private var value = 0
let step = 5
let range = 1...50
var body: some View {
Stepper(
value: $value,
in: range,
step: step
) {
Text("Current: \(value) in \(range.description) " +
"stepping by \(step)")
}
.padding(10)
}
}From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Controls.
- ButtonA control that initiates an action.
- ColorPickerA control used to select a color from the system color picker UI.
- DatePickerA control for selecting an absolute date.
- DisclosureGroupA view that shows or hides another content view, based on the state of a disclosure control.
- MultiDatePickerA control for picking multiple dates.
- OutlineGroupA structure that computes views and disclosure groups on demand from an underlying collection of tree-structured, identified data.
- PasteButtonA system button that reads items from the pasteboard and delivers it to a closure.
- PickerA control for selecting from a set of mutually exclusive values.
- SecureFieldA control into which people securely enter private text.
- SliderA control for selecting a value from a bounded linear range of values.
- SubscriptionViewA view that subscribes to a publisher with an action.
- TableA container that presents rows of data arranged in one or more columns, optionally providing the ability to select one or more members.