使用Swift更改UISlider图像

时间:2014-08-04 09:53:47

标签: ios xcode swift

我试图用Swift更改UISliders的默认图像。我在didFinishLaunchingWithOptions编辑AppDelegate

在Objective C中你会这样做:

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

我尝试过转换为Swift但未成功:

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

第一行很好,但第二行出错。

第二行的正确语法是什么?

由于

6 个答案:

答案 0 :(得分:8)

使用Swift更改UISlider拇指图像

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

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

答案 1 :(得分:5)

From Doc Example

let leftTrackImage = UIImage(named: "slider_blue_track")
customSlider.setMinimumTrackImage(leftTrackImage, forState: .Normal)

答案 2 :(得分:2)

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)
        }
    }
}

答案 3 :(得分:0)

使用swift4中的滑块快速更改图像

class ViewController: UIViewController {
    //var slider1 = UISlider()


    var immgar = [UIImage(named: "bgspices.jpg"),UIImage(named: "index1.jpg"),  UIImage(named: "index2.jpg"),UIImage(named: "index3.jpg"),UIImage(named: "index4.jpg"), UIImage(named: "woods.jpg")]




                  override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBOutlet weak var imageview: UIImageView!

    @IBOutlet weak var slider: UISlider!




    @IBAction func slider1(_ sender: Any) {


        print("changeImage method being called!!!!");

        let slider1 = Int(slider.value)
        imageview.image = immgar[slider1]

答案 4 :(得分:-1)

UIImage *img = [UIImage imageNamed:@"slider"];

UIImage *trackImg;

if (isiPad) 
{

[[UISlider appearance] setThumbImage:img forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:img forState:UIControlStateHighlighted];
trackImg = [UIImage imageNamed:@"ipad_img.png"];
}
else 
{

 [[UISlider appearance] setThumbImage:[self imageWithImage:img scaledToSize:CGSizeMake(27, 27)] forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:[self imageWithImage:img scaledToSize:CGSizeMake(27, 27)] forState:UIControlStateHighlighted];
 trackImg = [UIImage imageNamed:@"img.png"];
  }

[[UISlider appearance]  setMinimumTrackImage:trackImg forState:UIControlStateNormal];
[[UISlider appearance]  setMaximumTrackImage:trackImg forState:UIControlStateNormal];

答案 5 :(得分:-1)

对于Swift 3:

 mySlider.setThumbImage(UIImage(named: "icon_slider_thumb.png"), for: .normal)