如何将`accessibilityLabel`添加到`UIAlertView`按钮?

Lud*_*uda 6 accessibility objective-c uialertview ios

如何添加accessibilityLabelUIAlertView按钮?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" 
                                                message: @"message!"
                                               delegate: nil    
                                      cancelButtonTitle: @"cancelButton"  
                                      otherButtonTitles: @"otherButton"]; 

[alert show];
Run Code Online (Sandbox Code Playgroud)

Jam*_*ost 7

根据Apple的文档(搜索"使警报视图可访问"),AlertViews"默认可访问".这个以及按钮不可编辑的事实意味着您可能不应该尝试accessibilityLabel自己更改.默认情况下,他们使用按钮的标题和单词"按钮",这应该没问题.

警报视图的可访问性与警报标题,警报消息和按钮标题有关.如果VoiceOver被激活,当显示警报时它会说出"警告"一词,然后说出其标题,然后是设置的消息.当用户点击按钮时,VoiceOver会说出其标题和单词"按钮".当用户点击文本字段时,VoiceOver会说出其值和"文本字段"或"安全文本字段".


tan*_*one 1

执行此操作的唯一方法是在 UIAlertView 子视图中查找 UIButton:

for (id button in self.alertView.subviews){
    if ([button isKindOfClass:[UIButton class]]){
        ((UIButton *)button).accessibilityLabel = @"your custom text";
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,这是唯一的方法,因为没有公共 API 来访问这些 UIButton,这是因为 Apple 不希望您访问它们。Apple 不允许访问 UIAlertView 类的内部视图,这很可能会使您的应用程序在 App Store 审核过程中被拒绝。

如果您确实需要具有自定义accessibilityLabel 的UIButtons,您应该考虑设计一个自定义警报视图,而不是使用Apple UIAlertView 类。