Thumb image does not move to the edge of UISlider

后端 未结 9 1156
有刺的猬
有刺的猬 2021-02-13 02:14

Thumb image does not move to the edge even when it\'s value is max or min.

Does anyone know how to make it move all the way to the edge of the slider?

9条回答
  •  感情败类
    2021-02-13 02:51

    Most upvoted answer didn't work for me without changes. Here's what did -- it's a drop in replacement for UISlider:

    class Slider: UISlider {
    
        override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float)->CGRect {
    
                var thumbRect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
    
                //determine value dependent offset
                var offsetForValue: CGFloat = 0
                if value != 0 {offsetForValue = thumbRect.size.width * CGFloat(value / (self.maximumValue - self.minimumValue)) - ((value > 0) ? 2 : -2)}
    
                //apply offset to thumb rect
                thumbRect.origin.x += offsetForValue
    
                return thumbRect
        }
    }
    

提交回复
热议问题