-->

Showing / hiding accessibility elements in an over

2019-07-01 15:28发布

问题:

I’m implementing accessibility in a custom UITableViewCell class. I have a fairly simple overflow menu with a couple of buttons inside it, which are hidden until an ellipsis button is pushed that slides open and closes the overflow.

In my cell's initialiser I’m setting the accessibilityElementsHidden of my overflowContainer to YES. This seems to work, when scrolling through using VoiceOver, those views are skipped.

Then, when opening the cell, in the completion handler of the UIView animation, I set that same accessibilityElementsHidden of the same overflowContainer to NO. This doesn’t seem to have any effect, those elements are still skipped.

I’ve also tried posting UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil) before / after / when changing the accessibilityElementsHidden BOOL, but this also appears to have no effect on the situation.

Basically I’d like to toggle accessibilityElementsHidden on a couple of UIView instances at a specific point. Could anyone let me know what I may be doing wrong?

Here’s the code I fire when the overflow opens:

- (void)cellOverflowDidShow:(MyCell *)cell
{
    self.overflowContainer.isAccessibilityElement = YES;
    self.firstButton.isAccessibilityElement = YES;
    self.secondButton.isAccessibilityElement = YES;
    self.thirdButton.isAccessibilityElement = YES;
    UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.firstButton);
}

I fire the opposite when closing the cell (set all to NO and post notification again). And when initializing the cell, all I set is:

self.overflowContainer.isAccessibilityElement = NO;

Absolutely no idea why it shouldn’t be working, it appears I’m doing everything 100% correctly. If I don’t set the line in the initializer, the buttons all appear accessible (all the time). So it appears that the first call, be it YES or NO, works, but any subsequent ones are ignored.

回答1:

In the visible state, you declare the overflow container to be an accessibility element. Thus, VoiceOver will allow the user to focus it rather than navigate child elements. Instead of toggling whether it's an accessibility element, keep self.overflowContainer.isAccessibilityElement set to NO and toggle the accessibility of its children, firstButton, secondButton, and thirdButton.

A shorthand for setting the accessibility of child elements is accessibilityElementsHidden. Try setting self.overflowContainer.accessibilityElementsHidden to NO when the view appears and YES when it disappears.

You may still need to trigger a layout change notification, regardless.