启用VoiceOver时,在带有按钮的堆栈视图中设置不同的行为

时间:2018-10-04 17:39:45

标签: ios swift accessibility voiceover uiaccessibility

我有一个自定义堆栈视图,并排有两个按钮。启用VoiceOver时,我希望它将这两个按钮读取为某种选项卡,例如“ Button X Item 1 of 2”和“ Button Y Item 2 of 2”。有可能吗?

我的视图控制器具有以下内容:

@IBOutlet weak var buttonAdd: UIButton!
@IBOutlet weak var buttonDelete: UIButton!
@IBOutlet weak var selector: CustomSelectorStackView!

我尝试添加到我的viewDidLoad:

accessibilityElements = [buttonAdd, buttonDelete]

并更改按钮的特征,但没有成功。

我应该在viewDidLoad中添加哪些特征或其他可访问性元素,以便获得所需的输出。

1 个答案:

答案 0 :(得分:1)

您可以使用“辅助功能标签”属性来允许VoiceOver读取您想要的任何内容:

让按钮= [buttonAdd,buttonDelete]

for button in buttons {
    button.accessibilityLabel = "\(button), Item \(x) of \(buttons.count)"
}

您也可以将其添加为提示(但请注意,某些VoiceOver用户会关闭提示):

for button in buttons {
    button.accessibilityLabel = button
    button.accessibilityHint = "Item \(x) of \(buttons.count)"
}

这些示例假定VoiceOver用户可以理解可见的文本,例如“添加按钮”或“删除按钮”。如果文本无法理解,或者您使用图像代替文本,请使用accessibilityLabel分配可理解的文本。但是,在此示例中,您将无法遍历,必须将标签分别分配给每个按钮。然后,您可以将标签号附加到标签上,也可以进行迭代以将其分配给提示。

buttonAdd.accessibilityLabel = "Add Button"
buttonDelete.accessibilityLabel = "Delete Button"

从本质上讲,有多种方法可以完成您所描述的内容,而这些只是一些示例。

该指南非常有用: http://a11y-guidelines.orange.com/mobile_EN/dev-ios.html