如何在 swift UIKit 中公开用于自动化但不用于可访问性的元素?

问题描述 投票:0回答:1

我在容器视图中有两个视图,如下所示

@IBOutlet weak var dataDisclosureView: UIStackView! // Main ContainerView

@IBOutlet private weak var titleLabel: UILabel! {
        didSet {
            titleLabel.text = "Hello"
        }
    }
        
    @IBOutlet private weak var descriptionLabel: UILabel! {
        didSet {
            descriptionLabel.text = "World"
        }
    }
    
    @IBOutlet weak var descriptionView: UIStackView! { // sub container view containing titleLabel and descriptionLabel
        didSet {
            descriptionView.isAccessibilityElement = true
            descriptionView.accessibilityLabel = "Hello"
            descriptionView.accessibilityIdentifier = "test_hello"

        }
    }
    
    @IBOutlet private weak var requestButton: UIButton! {
        didSet {
            requestButton.isAccessibilityElement = true
            requestButton.accessibilityLabel = "Request Button"
            requestButton.accessibilityIdentifier = "test_button"
        }
    }

override func viewDidLoad() {
        super.viewDidLoad()
        dataDisclosureView.isAccessibilityElement = false
        dataDisclosureView.accessibilityElements = [ descriptionView ?? "" ]
        if #available(iOS 17.0, *) {
            dataDisclosureView.automationElements = [ descriptionView ?? "",
                                                      requestButton ?? ""]
        } else {
            // Fallback on earlier versions
        }
       let requestButtonAction = UIAccessibilityCustomAction(name: "start",
                                                          target: self,
                                                          selector: #selector( request))
    dataDisclosureView.accessibilityCustomActions = [ requestButtonAction ]
    }

我的问题是我想要

descriptionLabel
titleLabel
requestButton
hintLabel
(用于自动化)的 AccessibilityIdentifers 以及
descriptionView
requestButton
(VoiceOver 辅助功能)的辅助功能标签。

但是我无法在 AccessibilityInspector 中看到 Button、TitleLabel 和 descriptionLabel 的

accessibilityIdentifier
。我在这里做错了什么?

PS:我还关注了以下视频:

https://www.youtube.com/watch?v=IAqzXI3kFCk

这是一个可行的解决方案

 dataDisclosureView.accessibilityElements = [dataDisclosureView as Any,
                                           titleLabel as Any,
                                           descriptionLabel as Any,
                                           requestButton as Any,
                                           hintLabel as Any]
        if #available(iOS 17.0, *) {
            dataDisclosureView.automationElements =  [titleLabel as Any,
                                             descriptionLabel as Any,
                                             requestButton as Any,
                                             hintLabel as Any]
        } else {
            // Fallback on earlier versions
        }

但我对此并不满意,因为在 Apple WWDC 视频之一中提到,如果我们想要从语音辅助功能中排除项目,请不要将其添加到accessibilityItems中,如果我们想要在自动化元素中添加它,只需添加它即可。他们没有提供更深入的细节。参考是https://www.youtube.com/watch?v=IAqzXI3kFCk还添加了有关我所说内容的屏幕截图enter image description here

按照这个应该是

dataDisclosureView.accessibilityElements = [dataDisclosureView as Any,
                                           titleLabel as Any,
                                           descriptionLabel as Any,
                                           requestButton as Any]
        if #available(iOS 17.0, *) {
            dataDisclosureView.automationElements =  [titleLabel as Any,
                                             descriptionLabel as Any,
                                             requestButton as Any,
                                             hintLabel as Any]
        } else {
            // Fallback on earlier versions
        }
ios swift uikit accessibility
1个回答
0
投票

您通过设置

.isAccessibilityElement = false

使 StackView 对辅助功能不可见

如果您希望子视图可见,请尝试制作

dataDisclosureView.subviews.forEach { $0.isAccessibilityElement = true }

由于您正在执行

dataDisclosureView.accessibilityElements = [ descriptionView ?? "" ]
,因此只有descriptionView对于辅助功能检查器来说是可见的

© www.soinside.com 2019 - 2024. All rights reserved.