Search code examples
iosobjective-cuislider

UISlider with step like in Settings



I'm working on a Xcode project and I need to create a UISlider with 5 steps to rate something. I know this kind of slider exist natively because Apple uses it in Settings > General > Text Size. I can't post images but I hope you know what I'm speaking.

I searched on Google but I found nothing...
Thank you for your help !

PS: Sorry for my bad english but I'm french


Solution

  • Firstly, never assume something is available, simply because it exists in an apple developed app. This is far from the truth. Sadly, they have held back a great many things.

    To address your issue, try this (set slider min to 0 and max to 5 or whatever you want):

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
    
        [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
    }
    

    This should allow the slider to pop to 0,1,2,3,4, and 5. (don't forget to replace my variable names with your own)

    Hope that helps, happy coding.

    - (void)sliderChanged:(UISlider *)mySlider { 
         mySlider.value = round(mySlider.value); 
         self.valueLabel.text = [NSString stringWithFormat:@"%g", mySlider.value]; 
    }