I'm trying to change the height of a UISlider. This is what I am currently doing:
class CustomSlider: UISlider {
override func trackRect(forBounds bounds: CGRect) -> CGRect {
var newBounds = super.trackRect(forBounds: bounds)
newBounds.size.height = 20.0
return newBounds
}
@IBInspectable var thumbImage: UIImage? {
didSet {
setThumbImage(thumbImage, for: .normal)
}
}
@IBInspectable var thumbHighlightedImage: UIImage? {
didSet {
setThumbImage(thumbImage, for: .highlighted)
}
}
}
It seems to work for the most part, but I run into an issue that when the slider gets to the end, it is no longer rounded as you can see in the following image:
slider issue
Is there any way to resolve this? I'd prefer to stick with the system slider and not a custom one.
Edit: Changed image.
Swift 4.2
import UIKit
@IBDesignable open class DesignableSlider: UISlider {
@IBInspectable var trackHeight: CGFloat = 5
@IBInspectable var roundImage: UIImage? {
didSet{
setThumbImage(roundImage, for: .normal)
}
}
@IBInspectable var roundHighlightedImage: UIImage? {
didSet{
setThumbImage(roundHighlightedImage, for: .highlighted)
}
}
override open func trackRect(forBounds bounds: CGRect) -> CGRect {
//set your bounds here
return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: trackHeight))
}
}
*Don't forget to clean your project
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