Shakti
Shakti

Reputation: 996

How to change size of UISlider thumb image

here is my code but it covering my total slider frame i want to change only the slider icon it is not working

 [mySlider setThumbImage: [UIImage imageNamed:@"slider.png"]forState:UIControlStateNormal];
    mySlider.minimumValue = 2000;
    mySlider.maximumValue = 4000;
    mySlider.continuous = NO;
    [mySlider addTarget:self action:@selector(sliderChanged:)
       forControlEvents:UIControlEventValueChanged];

Upvotes: 0

Views: 5527

Answers (1)

Himanshu Patel
Himanshu Patel

Reputation: 1033

You need to set the initial thumb image using setThumbImage forState. This works fine for me in iOS 10 and returns the correct width of the image. The slider is setup in a storyboard and connected via an IBOutlet.

Looking at the properties that UISlider provides, you can get a pretty strong sense that while the class is designed to let you customize it's appearance, dynamically changing the appearance of the control as the user adjusts it probably wasn't what the designer had in mind. Otherwise, they'd have included the sort of mechanism you suggest: a means to associate different images with ranges of values.

self.thumbImage = [self imageWithImage:[UIImage imageNamed:@"IconForSlider.png"] scaledToSize:CGSizeMake(frame.size.height, frame.size.height)];
size = self.thumbImage.size.height;
[self setThumbImage:[self imageRotatedByDegrees:0] forState:UIControlStateNormal];
[self addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventValueChanged];

Upvotes: 1

Related Questions