Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UISlider Images with Swift

Tags:

xcode

ios

swift

I'm trying to change the default images for UISliders with Swift. I'm editing didFinishLaunchingWithOptions in AppDelegate.

In Objective C you would do this:

UIImage *maxImage = [UIImage imageNamed:@"slider-track.png"];
[[UISlider appearance] setMaximumTrackImage:maxImage forState:UIControlStateNormal];

I've tried converting to Swift but have not been successful:

var maxImage:UIImage = UIImage (named:"slider-track.png")
UISlider.setMaximumTrackImage(image: maxImage, forState: UIControlStateNormal)

The first line is fine, but the second gives an error.

What is the correct syntax for the second line?

Thanks

like image 997
user2428168 Avatar asked Aug 04 '14 09:08

user2428168


People also ask

How do you change the slider thumb on a picture?

Try setting the image name to @"sliderThumb. png" instead, as long as you have the legacy copy of the image. Show activity on this post. Just provide a different size of [email protected] and all we work itself out.

How do I assign an image in Swift?

First one is to drag and drop an image file from Finder onto the assets catalog. Dragging the image will automatically create new image set for you. Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”.


3 Answers

Changing UISlider thumb image with Swift

yourSlider.setThumbImage(UIImage(named: "yourSlider.png"), forState: UIControlState.Normal)

    yourSlider.setThumbImage(UIImage(named: "yourSlider.png"), forState: UIControlState.Highlighted)
like image 178
Krishna Gawade Avatar answered Sep 27 '22 20:09

Krishna Gawade


From Doc Example

let leftTrackImage = UIImage(named: "slider_blue_track")
customslider.setMinimumTrackImage(leftTrackImage, for: .normal)
like image 42
Kumar KL Avatar answered Sep 27 '22 21:09

Kumar KL


SWIFT 3

class Slider: UISlider {

    @IBInspectable var thumbImage: UIImage?

    // MARK: Lifecycle

    override func awakeFromNib() {
        super.awakeFromNib()

        if let thumbImage = thumbImage {
            self.setThumbImage(thumbImage, for: .normal)
        }
    }
}
like image 35
beatrizanso Avatar answered Sep 27 '22 19:09

beatrizanso