I'm trying to add accessibility to a simple UISlider
. I read the Apples adjustable document and saw that I need to implement two functions from the UIAccessibilityAction protocol
; accessibilityIncrement()
and accessibilityDecrement()
.
The problem I'm having is that even if I set the slider to be an accessibility element in viewDidLoad
and setting slider.accessibilityTraits = .adjustable
, the two override functions aren't called even if I change the values.
I also tried to set slider.accessibilityLabel = "test"
, but it's still not reading the label. Only how far the slider has come. For instance "80%".
Any idea on how I can make this work? I also read these two posts on stackOverflow, but none of them worked for me. accessibilityIncrement / Decrement not called and Accessibility accessibilityDecrement() not getting called
I can also mention that I also tried setting breakpoints at the accessibilityIncrement()
and accessibilityDecrement()
, but nothing happened.
override func viewDidLoad() {
super.viewDidLoad()
slider.isAccessibilityElement = true
slider.accessibilityLabel = "test"
slider.accessibilityTraits = .adjustable
}
override func accessibilityIncrement() {
slider.accessibilityValue = textField.text!
}
override func accessibilityDecrement() {
slider.accessibilityValue = textField.text!
}
@IBAction func sliderValueChanged(_ sender: UISlider) {
guard let unit = question?.unit else { return }
let currentValue = Int(sender.value + 0.5)
textField.text = "\(currentValue) \(unit)"
slider.accessibilityLabel = textField.text!
}
You implement the accessibilityIncrement()
and accessibilityDecrement()
methods in your view controller but they should belong to the created slider whose trait should be .adjustable
.
I suggest you take a look at this accessibility site where a complete example about adjustable values with code snippets and illustrations is provided for both ObjC and Swift.
Following this example will allow to call the accessibilityIncrement()
and accessibilityDecrement()
methods with your slider.