UISlider with increments of 5
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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.
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.
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
- '
UISlideris continuous by default and does not provide built-in step sizes.' - Snap to increments of
5by roundingvalue / stepand writing the result back. - Use
isContinuous = truefor live snapping orfalsefor 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
- UISplitViewController in portrait on iPhone shows detail VC instead of master
- UIStackView Hide View Animation
- UIStackView Hide View Animation
- UIStackView Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?
- UIStackView Unable to simultaneously satisfy constraints on squished hidden views
- UIStatusBarStyle not working in Swift
- UITableView - change section header color
- UITableView - scroll to the top
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.