Initializer

init(value:in:step:label:onEditingChanged:)

Creates a stepper configured to increment or decrement a binding to a value using a step value and within a range of values you provide.

Declaration 宣言

init<V>(value: Binding<V>, in bounds: ClosedRange<V>, step: V.Stride = 1, label: () -> Label, onEditingChanged: @escaping (Bool) -> Void = { _ in }) where V : Strideable
Available when Label conforms to View. LabelViewに準拠する場合に利用可能です。

Parameters パラメータ

value

A Binding to a value that you provide.

bounds

A closed range that describes the upper and lower bounds permitted by the stepper.

step

The amount to increment or decrement the stepper when the user clicks or taps the stepper’s increment or decrement buttons, respectively.

label

A view describing the purpose of this stepper.

onEditingChanged

A 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 onEditingChanged closure at the start and end of the gesture.

Discussion 議論

Use this initializer to create a stepper that increments or decrements a binding to value by the step size you provide within the given bounds. By setting the bounds, you ensure that the value never goes below or above the lowest or highest value, respectively.

The example below 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)
    }
}

A view displaying a stepper with a step size of five, and a

See Also 参照

Creating a Stepper Over a Range