Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell trouble with UIAccessibility elements

No matter what I try, I can't keep my custom UITableViewCell from acting like it should under the default rules for UIAccessiblity. I don't want this cell to act like an accessibility container (per se), so following this guide I should be able to make all of my subviews accessible, right?! It says to make each element accessible separately and make sure the cell itself is not accessible.

- (BOOL)isAccessibilityElement
{
    return NO;
}

- (NSString *)accessibilityLabel
{
    return nil;
}

- (NSInteger)accessibilityElementCount
{
    return 0;
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier //cells use this reusage stuff
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
    {
        [self setIsAccessibilityElement:NO];
        sub1 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,1,1)];
        [sub1 setAccessibilityLanguage:@"es"];
        [sub1 setIsAccessibilityElement:YES];
        [sub1 setAccessibilityLabel:sub1.text]

        sub2 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,1,1)];
        [sub2 setAccessibilityLanguage:@"es"];
        [sub2 setIsAccessibilityElement:YES];
        [sub2 setAccessibilityLabel:sub2.text]

The voice over system reads the contents of the whole cell all at once, even though I'm trying to stop that behavior. I could say

        [sub2 setIsAccessibilityElement:NO];

but that would would make this element entirely unreadable. I want to keep it readable, but not have the whole cell be treated like a container (and assumed to be the English language). There does not appear to be a lot of information out there on this, so at the very least I'd like to document it.

like image 535
ojreadmore Avatar asked Nov 09 '10 02:11

ojreadmore


2 Answers

If you have 2 separate elements (sub1 and sub2), you could override methods of the UIAccessibilityContainer informal protocol.

- (NSInteger)accessibilityElementCount {
    return 2;
}

- (id)accessibilityElementAtIndex:(NSInteger)index {
    if (index == 0) {
        return sub1;
    } else if (index == 1) {
        return sub2;
    }
    return nil;
}

- (NSInteger)indexOfAccessibilityElement:(id)element {
    if (element == sub1) {
        return 0;
    } else if (element == sub2) {
        return 1;
    }
    return NSNotFound;
}
like image 98
Jilouc Avatar answered Oct 04 '22 01:10

Jilouc


In iOS 8 or newer, one can simply set the accessibilityElements property:

// A list of container elements managed by the receiver.
// This can be used as an alternative to implementing the dynamic methods.
@available(iOS 8.0, *)
public var accessibilityElements: [AnyObject]?
like image 39
Rudolf Adamkovič Avatar answered Oct 04 '22 00:10

Rudolf Adamkovič