跳转至
Structure

Slider

A control for selecting a value from a bounded range.

Declaration

struct Slider<Label, ValueLabel> : View where Label : View, ValueLabel : View

Overview

A slider is a circle on a track. The user can move the circle from left to right to pick between values. The slider takes a binding that the user updates.

The most basic example looks like this:

struct SliderView: View {
    @State private var value: Double = 0

    var body: some View {
        Slider(value: $value)
    }
}
A gif displaying a light gray slider on a white background where the controlling point is being slid all the way from the edge of left side to that of the right and back again; blue color fills the slider to the left of the controlling point, shifting as the value changes.

In general, a slider has these four options:

  1. Add a label
  2. Change maximum and minimum values
  3. Create a step size
  4. Call a function when slider editing changes.

The slider's different initializers use different combinations of these options.

Availability

iOS 13.0+

macOS 10.15+

watchOS 6.0+

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(value:in:onEditingChanged:) Creates a slider.

init(value:in:onEditingChanged:label:) Creates a slider with a label.

init(value:in:onEditingChanged:minimumValueLabel:maximumValueLabel:label:) Creates a slider with a label and max/min labels.

init(value:in:step:onEditingChanged:) Creates a slider with a step size.

init(value:in:step:onEditingChanged:label:) Creates a slider with a label and a step size.

init(value:in:step:onEditingChanged:minimumValueLabel:maximumValueLabel:label:) Creates a slider with a label, max/min labels, and a step size.