Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing slider width : swift

I'm using UISlider in swift to show slider on my app. Is there a way by which we can increase the width of the slider ?

I have tried to transform the slider using CGAffineTransform. But this also increases the thumb size.

testslider.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 2))

I just need to increase width of the slider line but not the thumb size. Is there any way we can do that? Please suggest.

like image 680
Preetika Avatar asked Oct 24 '25 13:10

Preetika


1 Answers

Try the following code

    open class CustomSlider : UISlider {
    @IBInspectable open var trackWidth:CGFloat = 2 {
        didSet {setNeedsDisplay()}
    }

    override open func trackRect(forBounds bounds: CGRect) -> CGRect {
        let defaultBounds = super.trackRect(forBounds: bounds)
        return CGRect(
            x: defaultBounds.origin.x,
            y: defaultBounds.origin.y + defaultBounds.size.height/2 - trackWidth/2,
            width: defaultBounds.size.width,
            height: trackWidth
        )
    }
}
like image 87
Angel F Syrus Avatar answered Oct 27 '25 02:10

Angel F Syrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!