Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Background Image of UISlider?

Tags:

ios

uislider

I need to change the background of a UISlider, my goal is to create UISlider as the image below:

UISlider

I tried setBackgroundImage method, but it is like the frame of the slider.

How do I change the background image of the slider?

like image 867
erdemgc Avatar asked Nov 19 '13 11:11

erdemgc


2 Answers

1. Add image view with same dimension below your slider. Set your multicolor image to it.

2. Use transparent image as left and right of slider image

   UIImage *clearImage = [[UIImage imageNamed:@"clearSliderBar.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0];
    [slider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
    [slider setMaximumTrackImage:clearImage forState:UIControlStateNormal];

This is 20x8 sample transparet image for test

like image 143
Aditya Deshmane Avatar answered Nov 14 '22 17:11

Aditya Deshmane


Quick Swift 3 translation of the accepted answer. Remember to add your custom colors/view behind the slider before making the track images transparent.

let slider = UISlider()
let clearImage = UIImage().stretchableImage(withLeftCapWidth: 14, topCapHeight: 0)
slider.setMinimumTrackImage(clearImage, for: .normal)
slider.setMaximumTrackImage(clearImage, for: .normal)
like image 3
JAL Avatar answered Nov 14 '22 15:11

JAL