Taluca
Taluca

Reputation: 45

iOS Accessibility - is there a way to tell when VoiceOver has changed focus?

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?

Upvotes: 4

Views: 3671

Answers (2)

Justin
Justin

Reputation: 20609

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.

Upvotes: 1

Aaron Brager
Aaron Brager

Reputation: 66302

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.

Upvotes: 2

Related Questions