问题
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?
回答1:
Maybe, this way is easier.
cell.textLabel.accessibilityElementsHidden = YES;
Look this post
;)
回答2:
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
回答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
}
来源:https://stackoverflow.com/questions/23824305/how-to-disable-accessibility-for-custom-static-uitableviewcell