简体   繁体   中英

Disable VoiceOver on UIButton/UITableViewCell/UICollectionViewCell Selection

With VoiceOver switched-on, when focus comes on a UIButton / UITableViewCell / UICollectionViewCell , VoiceOver reads it's accessibility label once.

Then as soon as user double taps to select that UIButton / UITableViewCell / UICollectionViewCell , VoiceOver reads the same accessibility label again besides performing action (navigation etc) on UIButton / UITableViewCell / UICollectionViewCell selection.

I've searched a lot but not able to find a way to stop/disable VoiceOver reading accessibility label on UIButton / UITableViewCell / UICollectionViewCell selection.

Any help would be highly appreciated.

Swift 5

What worked for me was setting myElementIWantSilent.accessibilityTraits = .none

EDIT: I should note that these are also present:

viewContainingSilentElement.isAccessibilityElement = true
viewContainingSilentElement.accessibilityTraits = .image
viewContainingSilentElement.accessibilityLabel = "some text i want read aloud"

iPhone 8
iOS 14.5.1
XCode 12.5

Let's see how to stop the VoiceOver accessibility reading for the UIButton and the UITableViewCell elements.

UIBUTTON : just create your own button class and override the accessibilityActivate method.

class BoutonLabelDoubleTap: UIButton {

    override func accessibilityActivate() -> Bool {
        accessibilityLabel = ""
        return true
    }
}

UITABLEVIEWCELL : two steps to be followed.

  • Create a custom UIAccessibilityElement overriding the accessibilityActivate method.

     class TableViewCellLabelDoubleTap: UIAccessibilityElement { override init(accessibilityContainer container: Any) { super.init(accessibilityContainer: container) } override var accessibilityTraits: UIAccessibilityTraits { get { return UIAccessibilityTraitNone } set { } } override func accessibilityActivate() -> Bool { accessibilityLabel = "" return true } }
  • Use the previous created class to implement your table view cells in the view controller.

     class TestButtonTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet weak var myTableView: UITableView! @IBOutlet weak var bottomButton: UIButton! override func viewDidLoad() { super.viewDidLoad() myTableView.delegate = self as UITableViewDelegate myTableView.dataSource = self as UITableViewDataSource } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 2 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let zeCell = tableView.dequeueReusableCell(withIdentifier: "myPersoCell", for: indexPath) zeCell.accessibilityElements = nil var elements = [UIAccessibilityElement]() let a11yEltCell = TableViewCellLabelDoubleTap(accessibilityContainer: zeCell) a11yEltCell.accessibilityLabel = "cell number " + String(indexPath.row) a11yEltCell.accessibilityFrameInContainerSpace = CGRect(x: 0, y: 0, width: zeCell.contentView.frame.size.width, height: zeCell.contentView.frame.size.height) elements.append(a11yEltCell) zeCell.accessibilityElements = elements return zeCell } }

I haven't tried for a UICollectionViewCell but it should be the same rationale as the UITableViewCell 's.

Following these code snippets, you're now able to decide if VoiceOver should stop reading out the desired elements labels when selected .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM