跳转至
Structure

Stepper

Two side-by-side plus/minus buttons.

Declaration

struct Stepper<Label> : View where Label : View

Overview

Use a stepper to give user control incrementing or decrementing a discrete value.

  • Note: For changing a continuous value, use Slider instead. In general, there are 3 different stepper label types, and 3 different increment/decrement types, for a total of 9 different initializers.

Label types:

  1. String
  2. LocalizedStringKey
  3. View

Increment/decrement types:

  1. Increment and decrement actions
  2. Closed range binding
  3. Unlimited range binding

Here is a very simple example:

struct StepperView: View {
    @State private var value = 0

    var body: some View {
        Stepper(onIncrement: { value += 1 },
                onDecrement: { value -= 1 }) {
            Text("\(value)")
        }
    }
}
A gif displaying a stepper on the right side of the screen and a number on the left which reflects the value that the stepper has reached. When the user selects the plus button, the number is incremented by 1, and when the user selects the minus button, the number is decremented by 1. The initial value passed to the text is 0, a local state variable.

Availability

iOS 13.0+

macOS 10.15+

Topics


Instance Property

body The content and behavior of the view.


Type Alias

Body The type of view representing the body of this view.


Initializer

init(_:onIncrement:onDecrement:onEditingChanged:) Creates a stepper with a string label and increment/decrement actions.

init(_:onIncrement:onDecrement:onEditingChanged:) Creates a stepper with a localized string key binding and increment/decrement actions.

init(_:value:in:step:onEditingChanged:) Creates a stepper with a localized string key label and closed range binding.

init(_:value:in:step:onEditingChanged:) Creates a stepper with a string label and closed range binding.

init(_:value:step:onEditingChanged:) Creates a stepper with a string label and unlimited range binding.

init(_:value:step:onEditingChanged:) Creates a stepper with a localized string key label and unlimited range binding.

init(onIncrement:onDecrement:onEditingChanged:label:) Creates a stepper with a view label and increment/decrement actions.

init(value:in:step:onEditingChanged:label:) Creates a stepper with a view label and closed range binding.

init(value:step:onEditingChanged:label:) Creates a stepper with a view label and unlimited range binding.