0

I want to customize my UISlider to be able to show steps. I know this kind of slider exist natively because Apple uses it in Settings > General > Accessibility > Text Size.

I searched on Google but I found nothing.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You could take a look at [this](https://stackoverflow.com/questions/6370342/implementing-steps-snapping-uislider) question. – eirikvaa Apr 24 '19 at 05:51

1 Answers1

1

StoryBoard

enter image description here

StepSliderViewController Class

import UIKit

class StepSliderViewController: UIViewController {

    @IBOutlet weak var slder: UISlider!
    @IBOutlet weak var twoLeadSp: NSLayoutConstraint!
    @IBOutlet weak var threeLeadSp: NSLayoutConstraint!
    @IBOutlet weak var fiveLeadSp: NSLayoutConstraint!
    @IBOutlet weak var sixLeadSp: NSLayoutConstraint!


    var lastStep: Float = 0
    var stepValue: Float = 15

    override func viewDidLoad() {
        super.viewDidLoad()

        twoLeadSp.constant = -(slder.layer.frame.size.width / 6)
        threeLeadSp.constant = -((slder.layer.frame.size.width / 6) * 2)
        fiveLeadSp.constant = -((slder.layer.frame.size.width / 6) * 4)
        sixLeadSp.constant = -((slder.layer.frame.size.width / 6) * 5)

        self.slder.value = 45.0
        self.lastStep = (self.slder.value) / self.stepValue;
    }

    @IBAction func sliderChange(_ sender: Any) {
        let newStep = round((slder.value) / self.stepValue)
        self.slder.value = newStep * self.stepValue
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

Result

enter image description here

Ben Rockey
  • 920
  • 6
  • 23