Reputation: 4559
I'm trying to resize UISlider
thumb in iOS7 but I can't. I know that can use setThumbImage
but I don't want use any images.
setThumbImage:[UIImage imageNamed:@"sliderThumb.png"]
I want to change size and color of UISlider
thumb without image. can I do this?
Upvotes: 2
Views: 886
Reputation: 4507
The SWIFT code below enables you to easily change the size and color of a slider thumb:
override func viewDidLoad(){
super.viewDidLoad()
self.updateSlider(self.mySlider, thumbSize: 16, color: UIColor.redColor())
}
func updateSlider(slider: UISlider, thumbSize: CGFloat, color: UIColor){
let thumbImage = createThumbImage(thumbSize, color: color)
slider.setThumbImage(thumbImage, forState: UIControlState.Normal)
slider.setThumbImage(thumbImage, forState: UIControlState.Highlighted)
}
func createThumbImage(size: CGFloat, color: UIColor) -> UIImage {
let layerFrame = CGRectMake(0, 0, size, size)
let shapeLayer = CAShapeLayer()
shapeLayer.path = CGPathCreateWithEllipseInRect(layerFrame.insetBy(dx: 1, dy: 1), nil)
shapeLayer.fillColor = color.CGColor
shapeLayer.strokeColor = color.colorWithAlphaComponent(0.65).CGColor
let layer = CALayer.init()
layer.frame = layerFrame
layer.addSublayer(shapeLayer)
return self.imageFromLayer(layer)
}
func imageFromLayer(layer: CALayer) -> UIImage {
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, UIScreen.mainScreen().scale)
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return outputImage
}
Upvotes: 2