Increase touch target size of UISlider with Swift
Asked Answered
N

2

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!

Niggard answered 3/6, 2016 at 8:7 Comment(1)
Possible duplicate of Custom UISlider - Increase "hot spot" sizeIncontrollable
F
16

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.

Foamflower answered 3/6, 2016 at 8:16 Comment(1)
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!!Niggard
W
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)
     }
}
Watson answered 2/6, 2017 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.