Declaration
Initializers
6 SwiftUI initializers back <Slider>. The props you pass pick one.
<Slider
value: Binding<number>
in?: SwiftRange<number>
onEditingChanged?: (arg0: boolean) => void
/>nonisolated init<V>(
value: Binding<V>,
in bounds: ClosedRange<V> = 0...1,
onEditingChanged: @escaping (Bool) -> Void = { _ in }
)
where V : BinaryFloatingPoint, V.Stride : BinaryFloatingPointvalue*Binding<number>The selected value within
bounds.inSwiftRange<number>The range of the valid values. Defaults to
0...1.Default
`0...1`Swiftin bounds: ClosedRange<V>onEditingChanged(arg0: boolean) => voidA callback for when editing begins and ends.
Default
`{ _ in }`
Also takes the common view props: modifiers, style and testID.
Overview
A slider consists of a "thumb" image that the user moves between two extremes of a linear "track". The ends of the track represent the minimum and maximum possible values. As the user moves the thumb, the slider updates its bound value.
The following example shows a slider bound to the value speed. As the
slider updates this value, a bound Text view shows the value updating.
The onEditingChanged closure passed to the slider receives callbacks when
the user drags the slider. The example uses this to change the
color of the value text.
@State private var speed = 50.0
@State private var isEditing = false
var body: some View {
VStack {
Slider(
value: $speed,
in: 0...100,
onEditingChanged: { editing in
isEditing = editing
}
)
Text("\(speed)")
.foregroundColor(isEditing ? .red : .blue)
}
}You can also use a step parameter to provide incremental steps along the
path of the slider. For example, if you have a slider with a range of 0 to
100, and you set the step value to 5, the slider's increments would be
0, 5, 10, and so on. The following example shows this approach, and
also adds optional minimum and maximum value labels.
@State private var speed = 50.0
@State private var isEditing = false
var body: some View {
Slider(
value: $speed,
in: 0...100,
step: 5
) {
Text("Speed")
} minimumValueLabel: {
Text("0")
} maximumValueLabel: {
Text("100")
} onEditingChanged: { editing in
isEditing = editing
}
Text("\(speed)")
.foregroundColor(isEditing ? .red : .blue)
}The slider also uses the step to increase or decrease the value when a
VoiceOver user adjusts the slider with voice commands.
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.
- StepperA control that performs increment and decrement actions.
- 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.