6

I have a storyboard containing a UITableViewController with static content. The cells are very simple, containing just a single UILabel. If I now want to disable accessibility on one of the cells, I simply uncheck the mark on the label. This works as expected.

However if I now create an empty subclass of UITableViewCell and use this as the cell class for my static cell, accessibility will be enabled, ignoring all settings.

I tried overriding -isAccessibilityElement to return NO, programmatically setting all child views accessibilityElement property to NO, but it still will be selectable when using VoiceOver. The content won't be read by VoiceOver, only a single " " seems to be there (can be heard when swiping up/down on this element).

What do I need to do to disable accessibility for my custom cell?

Alfonso
  • 8,386
  • 1
  • 43
  • 63

3 Answers3

12

Maybe, this way is easier.

cell.textLabel.accessibilityElementsHidden = YES;

Look this post

;)

Community
  • 1
  • 1
debiasej
  • 980
  • 1
  • 14
  • 26
  • 1
    I'd just set accessibilityElementsHidden on the cell itself. You can even do it in Interface Builder with user-defined runtime attributes. – Ilya Sep 23 '16 at 09:13
3

Ok, I found a solution, though I'm not really happy with it.

To disable the cell as an accessibility element you need to turn it into an accessibility container without any elements:

@implementation CustomCell

- (BOOL)isAccessibilityElement {
    return NO; // prerequisite for being an accessibility container
}

- (NSInteger)accessibilityElementCount {
    return 0; // hack to disable accessibility for this cell
}

- (id)accessibilityElementAtIndex:(NSInteger)index {
    return nil;
}

- (NSInteger)indexOfAccessibilityElement:(id)element {
    return NSNotFound;
}

@end
Alfonso
  • 8,386
  • 1
  • 43
  • 63
3

In Swift

*Example code is Swift 3 but the crucial line of code to set accessibilityElementsHidden is not Swift 3 specific.

Before the cell (UITableViewCell) is displayed, you must set the cell's accessibilityElementsHidden property to true. This property indicates whether the accessibility elements contained within the accessibility element (in this case a cell) are hidden. accessibilityElementsHidden is false by default.

Within init()

The following code will set accessibilityElementsHidden true on initialization in a custom UITableViewCell subclass. This will work if the cell is created by storyboard, nib or created programmatically.

class CustomTableViewCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)
        self.accessibilityElementsHidden = true
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.accessibilityElementsHidden = true
    }
}

 

Within awakeFromNib()

If CustomTableViewCell will only be created from a storyboard or nib you could set the property in awakeFromNib().

class CustomTableViewCell: UITableViewCell {
    override func awakeFromNib() {
        self.accessibilityElementsHidden = true
    }
}

 

Within tableView(_:cellForRowAt:)

If you were creating and dequeuing the cells programmatically, the code looks like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // ... code that creates or dequeues the cell

    cell.accessibilityElementsHidden = true

    return cell
}
Mobile Dan
  • 6,444
  • 1
  • 44
  • 44