gpt4 book ai didi

swift - 分段选择器移除了可访问性

转载 作者:行者123 更新时间:2023-12-05 07:11:11 24 4
gpt4 key购买 nike

我正在尝试为我们的 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}}

在其他方面,Image with .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'

最佳答案

带有辅助功能标签的解决方案

对于带有图像的分段选择器的可访问性标签,我遇到了同样的问题。这是我如何让它工作的(在可重用的 View 中):

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 变量来保持对选择的引用。

关于swift - 分段选择器移除了可访问性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com