ios UI下一字段:右视图辅助功能(voiceOver)

cgvd09ve  于 2022-12-24  发布在  iOS
关注(0)|答案(1)|浏览(117)

我正在向UITextField的右视图添加两个UIButton,但voiceOver看不到它们(它只能“读取”包含视图)。我已将右视图的accessibilityElementsHidden设置为“no”。我不知道我做错了什么...
EDIT:我正在子类化UITextField,可以和这个相关吗?

slwdgvem

slwdgvem1#

// MARK: - VoiceOver handling
extension MyTextField {
    public var voiceOverElements: [UIAccessibilityElement] {
        guard UIAccessibility.isVoiceOverRunning else {
            return []
        }
        let element: UIAccessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
        let rect: CGRect = UIAccessibility.convertToScreenCoordinates(frame, in: self)
        element.accessibilityFrame = rect
        var result: [UIAccessibilityElement] = subviews.filter { $0 is UIButton }
            .map {
                let element: UIAccessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
                let rect: CGRect = UIAccessibility.convertToScreenCoordinates($0.frame, in: self)
                element.accessibilityFrame = rect
                return element
            }
        result.insert(element, at: 0)
        return result
    }

    public override class func isAccessibilityElement() -> Bool {
        false
    }

    public override func accessibilityElement(at index: Int) -> Any? {
        voiceOverElements[index]
    }

    public override func accessibilityElementCount() -> Int {
        voiceOverElements.count
    }

    public override func index(ofAccessibilityElement element: Any) -> Int {
        guard let element = element as? UIAccessibilityElement,
              let toRet = voiceOverElements.firstIndex(of: element) else {
            return 0
        }
        return toRet
    }
}

你应该重写UIAccessibleContainer方法来获取子视图可访问的框架。在这个例子中,我只获取了按钮,但是你可以省略它。

相关问题