UISlider Unusual Track Behaviour

I subclassed UISlider and changed the track width (and nothing else) and as you move the slider to the end the last bit of the track appears before the thumb has been moved to the end and there is no radius on the end. If the thumb is at the start you have to move the thumb quite a bit before the track starts to highlight.

Screenshot is useful but we should also need to see the entire subclass code.

class CustomSlider: UISlider {

    override func trackRect(forBounds bounds: CGRect) -> CGRect {

        let point = CGPoint(x: bounds.minX, y: bounds.midY)

        return CGRect(origin: point, size: CGSize(width: bounds.width, height: 20))

    }

}

I cannot reproduce. Are you sure you posted the whole code for the class ?

But why do you override trackRect ?

OK, I see it, but with smaller effect

That's because your subclassing is wrong.

If you try to add an IBAction to the slider, yet is not executed.

To subclass and redraw the slider, you have to do it in:

	override func draw(_ rect: CGRect) {

See examples here:

  • in objc

https://medium.com/@0209neha/creating-a-custom-uislider-7756bf898832

  • or here in swift

https://gist.github.com/Tulakshana/0b903747c20e04a14160882ce51230e3

UISlider Unusual Track Behaviour
 
 
Q