It is further assumed that you have a supervisor for which everything has the AccessableLayer class, but if you have a more complex layout, this scheme can be modified to deal with this.
To make CALayer accessible, you need a parent view that implements the UIAccessibilityContainer methods. Here is one suggested way to do this.
Each layer has its own UIAccessibilityElement
@interface AccessableLayer : CALayer @property (nonatomic) UIAccessibilityElement *accessibilityElement; @end
now in your implementation you change an element whenever it changes:
@implementation AccessableLayer ... self.accessibilityElement.accessibilityLabel = text; @end
AccessableLayer never creates a UIAccessibilityElement because the constructor requires a UIAccessibilityContainer. So create a superview and assign it:
#pragma mark - accessibility // The container itself is not accessible, so return NO - (BOOL)isAccessibilityElement { return NO; } // The following methods are implementations of UIAccessibilityContainer protocol methods. - (NSInteger)accessibilityElementCount { return [self.layer.sublayers count]; } - (id)accessibilityElementAtIndex:(NSInteger)index { AccessableLayer *panel = [self.layer.sublayers objectAtIndex:index]; UIAccessibilityElement *element = panel.accessibilityElement; if (element == nil) { element = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:self]; element.accessibilityFrame = [self convertRect:panel.frame toView:[UIApplication sharedApplication].keyWindow]; element.accessibilityTraits = UIAccessibilityTraitButton; element.accessibilityHint = @"some hint"; element.accessibilityLabel = @"some text"; panel.accessibilityElement = element; } return element; } - (NSInteger)indexOfAccessibilityElement:(id)element { int numElements = [self accessibilityElementCount]; for (int i = 0; i < numElements; i++) { if (element == [self accessibilityElementAtIndex:i]) { return i; } } return NSNotFound; }
source share