[英]segmented picker removes accessibility
我正在尝试为我们的 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: "") 的 Image 工作得很好,所以它必须与 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'
带有辅助功能标签的解决方案
对于带有图像的分段选择器的可访问性标签,我遇到了同样的问题。 这是我如何使它工作(在可重复使用的视图中):
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]!))
}
}
需要两个.accessibility
修饰符才能让 VoiceOver 在所有情况下说出辅助功能提示。 然后需要有一个显式的@State
或@Binding
变量来保持对选择的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.