Preetika
Preetika

Reputation: 804

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.

Upvotes: 4

Views: 1181

Answers (1)

Angel F Syrus
Angel F Syrus

Reputation: 2042

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

Upvotes: 2

Related Questions