Increase UISlider Touch Target Size Using Swift

I managed to increase my thumb size UISliderwith Swift, but the touch zone is still too small for my application.

How to programmatically increase the size of the sensory area for UISlider?

Do I have to implement my own slider?

Thank!

+4
source share
2 answers

Subclass UISliderand in your custom slider class 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 an object of this subclass when you use the slider.

If you used the slider in interfacebuilder(storyboard), set it as a class in this custom slider class from identity inspector.

+7

3 Lion :

import UIKit

class CustomSlider: UISlider {

     override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        var bounds: CGRect = self.bounds
        bounds = bounds.insetBy(dx: -10, dy: -15)
        return bounds.contains(point)
     }
}
+3

All Articles