How to change size of thumb image of UISlider programmatically

AndyYeung picture AndyYeung · Jun 27, 2012 · Viewed 20.4k times · Source

I would like to make the custom UISlider, something like this

|o----------| -> |-----O------| -> |------------〇|

the thumbImage will be small at the minimum value, it will increase the size during the slider value increase, otherwise it will decrease.

is anyone know how to do it?

Answer

Rui Peres picture Rui Peres · Jun 27, 2012

You can use this code:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

Taken from here.

The extra work you will have, will be a method A that will call the imageWithImage:scaledToSize: when the UISlider's value changes.