VoiceOver is finding a nearby accessible element to read?

天涯浪子 提交于 2019-12-02 04:07:46

A table view cell (container) and its content (children) cannot be accessible all together (explanation here).

Follow the steps hereunder to answer your edit mention so that only tapping on the label itself, not on its containing UITableViewCell, will read the label.

  • Create your own UITableViewCell class including one label.

    class TestTableViewCell: UITableViewCell {
    
        @IBOutlet weak var myLabel: UILabel!
    
        override var accessibilityTraits: UIAccessibilityTraits {
            get { return UIAccessibilityTraitNone }
            set {}
        }
    }
    
  • Define your label as a UIAccessibilityElement in the cell implementation to be included in its accessibilityElements array.

    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let zeCell = tableView.dequeueReusableCell(withIdentifier: "myPersoCell",
                                                       for: indexPath) as! TestTableViewCell
    
            zeCell.accessibilityElements = nil
    
            var elements = [UIAccessibilityElement]()
    
            let contentA11yElt = UIAccessibilityElement(accessibilityContainer: zeCell)
            contentA11yElt.accessibilityTraits = UIAccessibilityTraitStaticText
            contentA11yElt.accessibilityFrameInContainerSpace = zeCell.contentLabel.frame //To be adapted
            contentA11yElt.accessibilityLabel = "label content"
    
            elements.append(contentA11yElt)
            zeCell.accessibilityElements = elements
    
            return zeCell
        }
    

Following these code snippets will allow to select a label located inside a table view cell and to read out its content as desired.

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