Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 888
Jojo56400 Avatar asked Jun 03 '16 08:06

Jojo56400


2 Answers

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)
     }
}
like image 172
Peter Kreinz Avatar answered Oct 16 '22 10:10

Peter Kreinz


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.

like image 42
Ketan Parmar Avatar answered Oct 16 '22 08:10

Ketan Parmar