Search code examples
iosuislider

UISlider fixed position


I've set a UISlider value to 20 with a scale from MIN 0 to Max 100.

Is there a way to prevent the Slider from going below those initial 20?

Here is a picture to demonstrate what I want to achieve:

https://dl.dropboxusercontent.com/u/251368/bsp.png


Solution

  • Setup an action that handles changes to the slider's value. If the new value is less than 20 then reset the slider's value back to 20.

    Add an action:

    [slider addTarget:self action:@selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];
    

    Handle the change:

    - (void)sliderUpdate:(UISlider *)slider {
        CGFloat value = slider.value;
        if (value < 20) {
            slider.value = 20;
        }
    }