How to disable accessibility for custom static UITableViewCell

我怕爱的太早我们不能终老 提交于 2019-12-04 10:50:32
debiasej

Maybe, this way is easier.

cell.textLabel.accessibilityElementsHidden = YES;

Look this post

;)

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

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!