Jojo44 Jojo44 - 6 months ago 239
iOS Question

Increase touch target size of UISlider with Swift

I managed to increase the thumb size of my

UISlider
with
Swift
but the touch zone is still too small for my application.

How to programmatically increase the size of the touch zone for a
UISlider
?

Should I re-implement a custom slider by myself?

Thank you!

Answer

Subclass UISlider and in your custom class of slider add this method,

  func pointInside(_ point: CGPoint, withEvent event: UIEvent?) -> Bool {
    var bounds: CGRect = self.bounds
    bounds = CGRectInset(bounds, -10, -15)
    return CGRectContainsPoint(bounds, point)
}

Then create object of that subclass when you used slider.

If you have used slider in interfacebuilder (storyboard) then set it's class to that custom slider class from identity inspector.