iOS - Sliders

Use of Sliders

Sliders are used to choose a single value from a range of values.

Important Properties

  • continuous
  • maximumValue
  • minimumValue
  • value

Important Method

- (void)setValue:(float)value animated:(BOOL)animated 

Add Custom Methods addSlider and sliderChanged

-(IBAction)sliderChanged:(id)sender {
   NSLog(@"SliderValue %f",mySlider.value);
-(void)addSlider {
   mySlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 200, 200, 23)];
   [self.view addSubview:mySlider];
   mySlider.minimumValue = 10.0;
   mySlider.maximumValue = 99.0;
   mySlider.continuous = NO;
   [mySlider addTarget:self action:@selector(sliderChanged:) 

Update viewDidLoad in ViewController.m as follows −

(void)viewDidLoad {
   [super viewDidLoad];
   [self addSlider];


When we run the application, we'll get the following output −

iOS Tutorial

On dragging the slider, the output will be as follows and will print the new value in the console −

iOS Tutorial

It is used to allow users to make adjustments to a value or process throughout a range of allowed values.

Kickstart Your Career

Get certified by completing the course

Get Started