How it works
SliderTickBuilder is the result builder that assembles the tick marks you supply to a slider's ticks closure. When you want a slider to expose discrete, labeled stops rather than a continuous track, you describe those stops as a list of SliderTick values, and SliderTickBuilder collects them into the content the slider draws and snaps to. Reach for it whenever a Slider accepts a trailing ticks: closure and you need to declare where the marks fall along the value range.
Provide ticks through the slider's ticks: closure
A Slider that takes a ticks: closure routes that closure's body through SliderTickBuilder, so each statement you write becomes part of the tick collection. In the example, the
ticks:closure onSlider(value: $speed, in: 0...100, step: 10)is the builder context where the marks are declared.Declare each mark with SliderTick
Inside the builder, every individual stop is one SliderTick initialized with a position in the slider's value range. The example places three:
SliderTick(0),SliderTick(50), andSliderTick(100), pinning marks at the low end, midpoint, and high end of the0...100range.List marks without commas or a container
Because SliderTickBuilder is a result builder, you write the ticks as a sequence of expressions on their own lines rather than as an array or comma-separated list. The three
SliderTickcalls stack directly in the closure body, and the builder gathers them into the slider's set of ticks for you.Coordinate ticks with the slider's bound value
The marks the builder produces line up against the same range and binding that drive the track, so the slider can render and snap to them. Here the ticks sit along the
in: 0...100range that governs$speed, giving thespeedvalue visible reference points as it moves.
SliderTick(25) and SliderTick(75) line alongside the existing ticks to see the builder draw the new marks between the current ones.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct SliderTickBuilderDemo: View {
@State private var speed = 50.0
var body: some View {
VStack(spacing: 16) {
Text("Speed: \(Int(speed))")
.font(.headline)
Slider(value: $speed, in: 0...100, step: 10) {
Text("Speed")
} ticks: {
SliderTick(0)
SliderTick(50)
SliderTick(100)
}
}
.padding()
}
}