10

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!

Patrick
  • 1,629
  • 5
  • 23
  • 44
Jojo56400
  • 309
  • 3
  • 12
  • Possible duplicate of [Custom UISlider - Increase "hot spot" size](http://stackoverflow.com/questions/13196263/custom-uislider-increase-hot-spot-size) – Roland Keesom Jan 21 '17 at 15:22

2 Answers2

15

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.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • NIce! It works, but apparently the touch zone stay limited to the thumb size. If the touch zone is bigger than the thumb size, then it's reduced to the thumb size.Thanks!! – Jojo56400 Jun 03 '16 at 08:41
11

Swift 3 update of Lion's great answer:

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)
     }
}
Peter Kreinz
  • 7,979
  • 1
  • 64
  • 49