分段选择器去除无障碍功能

3
我将翻译以下内容,这是关于it技术的:“我正在尝试为我们的swift代码添加无障碍功能-以便进行自动化。最终目标是点击一个具有唯一标识符的按钮。”
“当前实现的样子如下:”
var body: some View {
   NavigationView {
      ZStack {
         VStack {
            HStack{
               Picker(selection: _ , label: Text("")) {
                  Image(systemName: "list.bullet").tag().accessibility(identifier: "list")
                  Image(systemName: "square.grid.3x2.fill").tag().accessibility(identifier: "grid")

               }
               .pickerStyle(SegmentedPickerStyle())
            }
         }
      }
   }
}

我相信当.pickerStyle转换成分段控制器并且图片变成按钮时,Xcode会删除所有可访问性特征。应用程序的控制台输出如下:
 SegmentedControl, 0x60000121e4c0, {{668.0, 90.0}, {150.0, 32.0}}
        Button, 0x60000121e5a0, {{668.0, 90.0}, {74.0, 32.0}}, Selected
        Button, 0x60000121e680, {{743.0, 90.0}, {75.0, 32.0}}

在其他方面,具有.accessibility(identifier: "")的图像完美地工作,因此它必须与pickerStyle有关。
我还尝试了这些无障碍属性:
.accessibility(hidden: false)
.accessibility(label: "")
.accessibility(value: "")

有人知道如何解决这个问题吗? 最终,调试器可以打印出:

 SegmentedControl, 0x60000121e4c0, {{668.0, 90.0}, {150.0, 32.0}}
        Button, 0x60000121e5a0, {{668.0, 90.0}, {74.0, 32.0}}, identifier: 'list', Selected
        Button, 0x60000121e680, {{743.0, 90.0}, {75.0, 32.0}}, identifier: 'grid'

在我的情况下也是这样。你能解决它吗? - damirstuhec
1个回答

0

使用可访问标签的解决方案

我在带有图像的分段选择器的可访问标签方面遇到了同样的问题。以下是我如何使其工作的方法(在可重用视图中):

enum Choice: CaseIterable, Hashable {
    case one, two, three
}

struct MyPickerView: View {
    @Binding var choice: Choice

    static let choices: [Choice: String] = [
        .one: "1.circle",
        .two: "2.circle",
        .three: "3.circle"
    ]

    static let hints: [Choice: LocalizedStringKey] = [
        .one: "One item",
        .two: "Two items",
        .three: "Three items"
    ]

    var body: some View {
        Picker(selection: $choice.animation(), label: Text("")) {
            ForEach(Choice.allCases, id: \.self) { choice in
                Image(systemName: Self.choices[choice]!)
                    .tag(choice)
                    // Needed for VoiceOver on unselected items
                    .accessibility(label: Text(Self.hints[choice]!))
            }
        }
        .pickerStyle(SegmentedPickerStyle())
        // Needed the first time VoiceOver is on the selected item
        .accessibility(label: Text(Self.hints[self.choice]!))
    }
}

为了让VoiceOver在所有情况下都能够发出辅助提示,需要同时使用.accessibility修饰符。因此,需要有一个显式的@State@Binding变量来保持对选择的引用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接