Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change size of thumb image of UISlider programmatically

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?

like image 487
AndyYeung Avatar asked Jun 27 '12 08:06

AndyYeung


People also ask

How do you change the slider thumb size in Swift?

You can't change the size of the default thumb image, but UISlider has a method setThumbImage(_:for:) that will allow you to pass a similar, smaller image. See Customizing the Slider's Appearance of the API Reference.


2 Answers

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.

like image 118
Rui Peres Avatar answered Oct 22 '22 03:10

Rui Peres


Swift 3:

extension UIImage {

    func scaleToSize(newSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        return newImage
    }
}
like image 28
Teodor Ciuraru Avatar answered Oct 22 '22 03:10

Teodor Ciuraru