UISlider
SwiftUI
iOS Development
Swift Programming
Increment Steps

UISlider with increments of 5

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

UISlider is continuous by default, so values such as 13.7 or 42.1 are completely normal unless you intervene. If you want the slider to move in increments of 5, the standard solution is to round the current value to the nearest step and then write that snapped value back to the slider.

The Core Math

The step calculation is simple:

snapped = round(rawValue / step) * step

If the step size is 5, then values near 10 snap to 10, values near 15 snap to 15, and so on.

UIKit Example

Here is a complete UIKit example that snaps to increments of 5 while the user drags.

swift
1import UIKit
2
3final class SliderViewController: UIViewController {
4    private let slider = UISlider()
5    private let valueLabel = UILabel()
6    private let step: Float = 5
7
8    override func viewDidLoad() {
9        super.viewDidLoad()
10        view.backgroundColor = .systemBackground
11
12        slider.minimumValue = 0
13        slider.maximumValue = 100
14        slider.isContinuous = true
15        slider.addTarget(self, action: #selector(sliderChanged(_:)), for: .valueChanged)
16
17        valueLabel.textAlignment = .center
18
19        slider.translatesAutoresizingMaskIntoConstraints = false
20        valueLabel.translatesAutoresizingMaskIntoConstraints = false
21
22        view.addSubview(slider)
23        view.addSubview(valueLabel)
24
25        NSLayoutConstraint.activate([
26            slider.centerXAnchor.constraint(equalTo: view.centerXAnchor),
27            slider.centerYAnchor.constraint(equalTo: view.centerYAnchor),
28            slider.widthAnchor.constraint(equalToConstant: 260),
29            valueLabel.topAnchor.constraint(equalTo: slider.bottomAnchor, constant: 20),
30            valueLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
31        ])
32
33        updateLabel(with: slider.value)
34    }
35
36    @objc
37    private func sliderChanged(_ sender: UISlider) {
38        let snapped = round(sender.value / step) * step
39        sender.value = snapped
40        updateLabel(with: snapped)
41    }
42
43    private func updateLabel(with value: Float) {
44        valueLabel.text = "Value: \(Int(value))"
45    }
46}

This gives the user a stepping effect even though the control itself is fundamentally continuous.

Snap Only When Dragging Ends

Sometimes snapping on every movement feels jumpy. In that case, let the slider move freely while dragging and snap only when the user releases it.

swift
slider.isContinuous = false
slider.addTarget(self, action: #selector(sliderChanged(_:)), for: .valueChanged)

With isContinuous = false, the action is sent when the interaction completes instead of during every intermediate movement.

Choosing Minimum and Maximum Values

Stepped sliders work best when the range lines up cleanly with the step size. For example, 0 to 100 with step 5 is clean. A range like 3 to 97 with step 5 still works, but the edge behavior is less intuitive because the endpoints are not natural multiples of the step.

If the UI should only allow discrete choices, design the range and step together instead of treating the step size as an afterthought.

A Reusable Snapping Helper

If you use stepped sliders in several places, move the math into a helper.

swift
1func snap(_ value: Float, step: Float) -> Float {
2    return round(value / step) * step
3}
4
5print(snap(12, step: 5))
6print(snap(18, step: 5))

Centralizing the snapping rule makes it easier to test and reuse.

SwiftUI Still Uses the Same Idea

Even though the title is about UISlider, the same stepping logic applies when a SwiftUI Slider is backed by a continuous range. The control may look different, but the core behavior is still "read a floating-point value, snap it, then display or store the snapped result."

That is useful if you have mixed UIKit and SwiftUI code in the same app.

Common Pitfalls

The biggest pitfall is assuming UISlider has a built-in step-size property. It does not. Snapping is application logic you add yourself.

Another issue is updating the slider label from the raw unsnapped value while the slider itself is being snapped. That makes the UI look inconsistent.

Developers also forget about isContinuous. Snapping during every movement and snapping only at the end are different interaction styles, and the choice should be deliberate.

Finally, watch out for floating-point expectations. The snapped result may still be stored as a Float, so format the displayed value appropriately if you want clean integers.

Summary

  • 'UISlider is continuous by default and does not provide built-in step sizes.'
  • Snap to increments of 5 by rounding value / step and writing the result back.
  • Use isContinuous = true for live snapping or false for snap-on-release behavior.
  • Choose slider ranges that make sense with the chosen step size.
  • Keep display values and stored values aligned with the snapping rule.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.