Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISlider with different colors

I have a slider with min value 0 and max value 5(be any value) , i need to display 5 different colors in the same slider ie 0-1 one color, 1-2 another color and so on. Is this possible . Please help me if any idea.

like image 449
krishna Avatar asked May 03 '13 07:05

krishna


2 Answers

Try this,

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0f, 400.0f, 320.0f, 35.0f)];
[slider setMinimumTrackTintColor:[UIColor redColor]]; // Initial color of selection
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[[self view] addSubview:slider];

In target method, you can set the colors

- (void)sliderValueChanged:(UISlider *)slider {

/* 

 Here you check the value of slider from [slider value] call;

 then set the color of slider by using

 [slider setMinimumTrackTintColor:[UIColor redColor]];

 */
}

Hope this will help you :)

like image 57
Augustine P A Avatar answered Sep 21 '22 22:09

Augustine P A


you will have to use images for that and in the

-(IBAction)sliderValueChanged:(UISlider *)sender
{
 // depending on the value 
 // if value is 1 {
    [mySlider setMinimumTrackImage:[UIImage imageNamed:@"leftImageforValue=1.png"] forState:UIControlStateNormal];
    [mySlider setMaximumTrackImage:[UIImage imageNamed:@"rightImageforValue=1.png"] forState:UIControlStateNormal];
}
like image 36
Bonnie Avatar answered Sep 20 '22 22:09

Bonnie