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
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.
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”.
Changing UISlider thumb image with Swift
yourSlider.setThumbImage(UIImage(named: "yourSlider.png"), forState: UIControlState.Normal)
yourSlider.setThumbImage(UIImage(named: "yourSlider.png"), forState: UIControlState.Highlighted)
From Doc Example
let leftTrackImage = UIImage(named: "slider_blue_track")
customslider.setMinimumTrackImage(leftTrackImage, for: .normal)
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With