How do I add accessibilityLabel
to UIAlertView
buttons?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement"
message: @"message!"
delegate: nil
cancelButtonTitle: @"cancelButton"
otherButtonTitles: @"otherButton"];
[alert show];
According to Apple's documentation (search for 'Making Alert Views Accessible'), AlertViews are 'accessible by default'. This, and the fact that the buttons aren't editable, means that you probably shouldn't try changing the accessibilityLabel
s yourself. By default they use the button's title and the word 'button', which should be fine.
Accessibility for alert views pertains to the alert title, alert message, and button titles. If VoiceOver is activated, it speaks the word “alert” when an alert is shown, then speaks its title followed by its message if set. As the user taps a button, VoiceOver speaks its title and the word “button.” As the user taps a text field, VoiceOver speaks its value and “text field” or “secure text field.”
The only way you can do this is finding the UIButtons within the UIAlertView subviews:
for (id button in self.alertView.subviews){
if ([button isKindOfClass:[UIButton class]]){
((UIButton *)button).accessibilityLabel = @"your custom text";
}
}
However this is the only way to do it because there is not a public API to access these UIButtons and this is because Apple doesn't want you to access them. Accessing internal views of the UIAlertView class is something which Apple does not allow and it is likely that will make your app rejected during the App Store review process.
If you really need to have UIButtons with a custom accessibilityLabel you should look into designing a custom alert view instead of using the Apple UIAlertView class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With