iOS programmatically create UISlider

UISlider is a view that is displays a horizontal bar to the user with an indicator for the current value. The slider needs minimum and maximum values and you can set the initial value from where the indicator will start. With the help of the target action for a particular event you can listen for changes to the slider. In this example we will also review how to create a Step Slider where we only allow certain values to be set in the slider.

iOS programmatically create UISlider

Interface file for the view controller - MySliderViewController.h

#import <UIKit/UIKit.h>

@interface MySliderViewController : UIViewController

@property (nonatomic, strong) UISlider *mySlider, *myStepSlider;

@end

Implementation file for the view controller - MySliderViewController.m

#import "MySliderViewController.h"

@interface MySliderViewController ()

@end

@implementation MySliderViewController

@synthesize mySlider, myStepSlider;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    
    //set the view background to white
    self.view.backgroundColor = [UIColor whiteColor];
    
    //frame for the slider
    CGRect myFrame = CGRectMake(10.0f, 10.0f, 250.0f, 25.0f);
    //create and initialize the slider
    self.mySlider = [[UISlider alloc] initWithFrame:myFrame];
    //set the minimum value 
    self.mySlider.minimumValue = 0.0f;
    //set the maximum value
    self.mySlider.maximumValue = 100.0f;
    //set the initial value
    self.mySlider.value = 25.0f;
    
    //set this to true if you want the changes in the sliders value
    //to generate continuous update events
    [self.mySlider setContinuous:false];
    
    //attach action so that you can listen for changes in value
    [self.mySlider addTarget:self
                      action:@selector(getSliderValue:)
                    forControlEvents:UIControlEventValueChanged];
    //add the slider to the view
    [self.view addSubview:self.mySlider];
    
    
    //move the origin for the Step Slider
    myFrame.origin.y += myFrame.size.height + 20.0f;
    self.myStepSlider = [[UISlider alloc] initWithFrame:myFrame];
    self.myStepSlider.minimumValue = 0.0f;
    self.myStepSlider.maximumValue = 100.0f;
    self.myStepSlider.value = 30.0f;
    [self.myStepSlider setContinuous:false];
    [self.myStepSlider addTarget:self
                      action:@selector(getSliderValue:)
            forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.myStepSlider];

    
}

- (void) getSliderValue:(UISlider *)paramSender{
    
    //if this is my Step Slider then change the value 
    if ([paramSender isEqual:self.myStepSlider]){
        float newValue = paramSender.value /10;
        paramSender.value = floor(newValue) * 10;
    }
    NSLog(@"Current value of slider is %f", paramSender.value);
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Reference