TechnologiesSwiftUIPickers and Text Fields

SliderTickContent protocol

iOSmacOStvOSwatchOSvisionOS✓ renders

A type that provides content for a `SliderTickBuilder`.

How it works

SliderTickContent is the protocol that describes the tick marks drawn along a slider's track. When you supply a tick builder to a Slider, the views you produce inside it conform to SliderTickContent, letting SwiftUI lay out, position, and style the marks that annotate the slider's range. Reach for it when a plain continuous slider isn't enough and you want visible, value-anchored reference points — for example, to show the discrete stops of a stepped control or to call out meaningful values along the track.

  1. Produce tick content inside a Slider's tick builder

    A Slider accepts a closure whose result conforms to SliderTickContent; whatever views you return there become the track's tick marks. In the example the builder is the trailing closure passed alongside Slider(value: $volume, in: 0...10, step: 1), and its body resolves to SliderTickContent.

  2. Place a mark at a value with SliderTick

    The primitive that conforms to SliderTickContent is SliderTick, which takes the position on the slider's value axis where the mark should sit. Here SliderTick(Double(i)) anchors a tick to each integer value, so the mark for i lands at exactly that point within the 0...10 range.

  3. Generate many ticks with ForEach

    Because SliderTickContent composes like other SwiftUI content, you can drive it with ForEach to emit a tick per value instead of writing each one by hand. ForEach(0..<11) walks the eleven stops and yields a SliderTick for each, producing the full row of marks across the track.

  4. Align ticks with the slider's step

    Tick positions are interpreted in the slider's own coordinate space, so they read most clearly when they match its step. With step: 1 over 0...10, the eleven SliderTick marks line up with the exact values the thumb can snap to, turning each tick into a labeled snap target.

Try it — Change ForEach(0..<11) to ForEach(0..<6) and emit SliderTick(Double(i) * 2) so ticks appear only at 0, 2, 4, 6, 8, 10, and watch how SliderTickContent thins the marks to every other value.

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.

SliderTickContent.swift
struct SliderTickContentDemo: View {
    @State private var volume = 5.0

    var body: some View {
        VStack(spacing: 16) {
            Text("Volume: \(Int(volume))")
                .font(.headline)
            Slider(value: $volume, in: 0...10, step: 1) {
                ForEach(0..<11) { i in
                    SliderTick(Double(i))
                }
            }
        }
        .padding()
    }
}
Live preview
Volume: 5
Volume: 5
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →