4

I'd like to call a method every time a different element is focused while VoiceOver is active. I was hoping there would be some UIAccessibilityNotification for this, but I can't seem to find any.

Ultimately, my goal is to add an additional condition prior to reading the accessibility label. For example, as opposed to saying (by default) "If UIButton becomes focused: read label", I'd like to be able to say "When UIButton becomes focused AND UIButton's background color is blue: read label".

So my question is: how do I either add an additional condition prior to reading the label, or receive a notification when a new element becomes focused?

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Taluca
  • 45
  • 2
  • 8

2 Answers2

2

You can't explicitly tell when the user moves the VoiceOver cursor (just like you can't tell where a sighted user is looking).

For the behavior you want, you have two options:

  1. Set the button's accessibilityLabel to an appropriate value whenever the other conditions change.
  2. Subclass UIButton and override its accessibilityLabel getter method:

    - (NSString *) accessibilityLabel {
        if (SOME_CONDITION) {
            return @"Hooray!";
        } else {
            return @"Womp womp";
        }
    }
    

If you need to disable an item entirely, rather than returning nil or a blank string, you should set its accessibilityElementsHidden property to YES.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
1

You can use the UIAccessibilityFocus protocol to detect changes in focus by accessibility clients (including VoiceOver). Note that UIAccessibilityFocus is an informal protocol that each accessibility element must implement independently.

That said, for your use case, Aaron is right to suggest returning a different accessibilityLabel under each condition.

Justin
  • 20,509
  • 6
  • 47
  • 58
  • The example I used doesn't actually apply to my situation, just an attempt to explain my issue and goals. That being said, I appreciate your suggestion regarding `UIAccessibilityFocus` and I will definitely look into it. Thanks in advance. – Taluca Apr 24 '15 at 03:44
  • What exactly are you trying to achieve Taluca? The suggestion by Justin definitely would solve the problem you outline, but if that's not the problem you are trying to solve, it's difficult to provide a more accurate answer. – Kasper Munck May 01 '15 at 11:30