如何在iOS中更改UISlider thumb image的大小?

时间:2017-03-27 01:55:23

标签: ios swift uislider

我想在32x32中设置UISlider拇指的大小,但拇指没有显示正确的大小或正确的形状。

我该如何解决?

code1:尝试调整UIImage的大小

let slider1 = UISlider(frame: CGRect(x:140, y:140, width:274/2, height:32))
slider1.minimumValue = 0
slider1.maximumValue = 1
slider1.value = 0.5
slider1.minimumTrackTintColor = UIColor.clear

let upThumbImage : UIImage = UIImage(named: "Handle_64x64")!
let size = CGSize(width: 32, height: 32)
UIGraphicsBeginImageContext(size)
upThumbImage.draw(in: CGRect(x:0, y:0, width:size.width, height:size.height))
let resizeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

slider1.setThumbImage(resizeImage, for: .normal)

slider1.addTarget(self, action: #selector(self. slider1Changed(sender:)), for: UIControlEvents.valueChanged)
upView.addSubview(slider1)

结果:尺寸正确,但图像分辨率不好

result1:

code2:尝试将图片设置为UIColor

let slider1 = UISlider(frame: CGRect(x:140, y:140, width:274/2, height:32))
slider1.minimumValue = 0
slider1.maximumValue = 1
slider1.value = 0.5
slider1.minimumTrackTintColor = UIColor.clear

let thumbTintColor = UIColor(patternImage: UIImage(named: "Handle_64x64")!)
slider1.thumbTintColor = thumbTintColor

slider1.addTarget(self, action: #selector(self. slider1Changed(sender:)), for: UIControlEvents.valueChanged)
upView.addSubview(slider1)

结果:尺寸和形状不正确

result2

code3:使用原始大小为32x32的图像  (这次我使用不同的图像,但大小为32x32)

let slider1 = UISlider(frame: CGRect(x:140, y:140, width:274/2, height:32))
    slider1.minimumValue = 0
    slider1.maximumValue = 1
    slider1.value = 0.5
    slider1.minimumTrackTintColor = UIColor.clear

    let thumbTintColor = UIColor(patternImage: UIImage(named: "Handle_32x32")!)
    slider1.thumbTintColor = thumbTintColor

    slider1.addTarget(self, action: #selector(self. slider1Changed(sender:)), for: UIControlEvents.valueChanged)
    upView.addSubview(slider1)

结果:尺寸和形状不正确

result3

0 个答案:

没有答案