ToolbarItem 中按钮中图像的可访问性

Geo*_*e_E 4 accessibility swift swiftui voice-control

accessibilityLabel(_:)我正在将辅助功能添加到我的 SwiftUI 应用程序中,直到我在将 an 添加到Buttona 中时遇到问题ToolbarItem。这是一些示例代码:

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            Text("Content")
                .accessibilityElement()
                .accessibilityLabel("Content label") // This is here just to show Voice Control is working
                .navigationTitle("Test")
                .toolbar {
                    // Comment parts out below, depending on what you want to test
                    ToolbarItem(placement: .navigationBarTrailing) {
                        // What I want, but doesn't work:
                        // (Also tried adding the label to either the button
                        // label or the whole button itself, neither works.)
                        Button {
                            print("Pressed")
                        } label: {
                            Image(systemName: "plus")
                                .accessibilityElement()
                                .accessibilityLabel("Some label")
                        }
                        .accessibilityElement()
                        .accessibilityLabel("Some other label")
                        
                        // What I don't want, but does work:
                        Image(systemName: "plus")
                            .accessibilityLabel("Another label")
                    }
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在测试语音控制的可访问性。奇怪的是,辅助功能标签适用于工具栏项目中的图像,但不适用于工具栏项目中的按钮内。

当我说辅助功能标签不起作用时,它说的"Add"是而不是预期的标签。我假设 SwiftUI 默认为系统映像“plus”创建此标签,但我想更改它。

按钮辅助功能标签不在工具栏项目中时也可以使用。这是一个错误,还是我造成的某些问题?

paw*_*222 6

SwiftUI 以不同的方式对待单个工具栏项目(应用它们自己的样式、大小等)。看起来这也适用于可访问性标签。

幸运的是,有一个解决方法 - 请参阅SwiftUI Xcode 12.3 无法更改工具栏中的按钮大小

对于您的情况,代码应如下所示:

.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        HStack {
            Text("")
                .accessibilityHidden(true)
            
            Button {
                print("Pressed")
            } label: {
                Image(systemName: "plus")
                    .accessibilityElement()
                    .accessibilityLabel("Some label")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

accessibilityLabel可以附加到Image或 上Button。)

使用 Xcode 12.3、iOS 14.3 进行测试。

  • 当我测试此解决方案时,生成的辅助功能元素没有按钮特征。您也可以考虑添加“.accessibility(addTraits: .isButton)”。 (2认同)