TechnologiesSwiftUI

SliderTickBuilder struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A result builder that constructs `SliderTick`s for use when creating

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.

  1. 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 on Slider(value: $speed, in: 0...100, step: 10) is the builder context where the marks are declared.

  2. 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), and SliderTick(100), pinning marks at the low end, midpoint, and high end of the 0...100 range.

  3. 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 SliderTick calls stack directly in the closure body, and the builder gathers them into the slider's set of ticks for you.

  4. 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...100 range that governs $speed, giving the speed value visible reference points as it moves.

Try it — Add a 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.

SliderTickBuilder.swift
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()
    }
}
Live preview
Speed: 50
Speed: 50
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →