简体   繁体   中英

Hide UISlider thumb image

I am trying to create a UISlider without the thumb image.

How can I do this, this is my code so far:

UISlider *sli = [[UISlider alloc] initWithFrame:progressView.frame];
    [sli setThumbImage:nil forState:UIControlStateNormal];
    [sli setBackgroundColor:[UIColor clearColor]];

    [sli setMinimumTrackImage:[[UIImage imageNamed:@"ProgressBlueCap.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [sli setMaximumTrackImage:[[UIImage imageNamed:@"ProgressBlueCapRight.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];

Much simpler:

Objc

[sli setThumbImage:[[[UIImage alloc] init] autorelease] forState:UIControlStateNormal];

Swift version

sli.setThumbImage(UIImage(), for: .normal)

The easiest way is simply setting the Thumb Tint colour to Clear on the Interface Builder - like so...

在此输入图像描述

Voila!

This is an old thread but I found it so someone else may as well.

Here's a solution to this in Swift. I'm creating a blank UIImage here programatically and then assigning it to the thumb.

    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0)
    UIColor.clear.setFill()
    UIRectFill(rect)
    if let blankImg = UIGraphicsGetImageFromCurrentImageContext() {
        slider.setThumbImage(blankImg, for: .normal)
    }
    UIGraphicsEndImageContext()

一线解决方案:

[_slider setThumbImage:[UIImage new] forState:UIControlStateNormal];

I'd try subclassing UISlider and override -thumbRectForBounds:trackRect:value: to return NSZeroRect. If that doesn't do it, try overriding -thumbImageForState: to return nil.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM