Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add `accessibilityLabel` to `UIAlertView` buttons?

How do I add accessibilityLabel to UIAlertView buttons?

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

[alert show];
like image 761
Luda Avatar asked Jan 30 '14 13:01

Luda


Video Answer


2 Answers

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 accessibilityLabels 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.”

like image 100
James Frost Avatar answered Oct 18 '22 04:10

James Frost


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.

like image 36
tanzolone Avatar answered Oct 18 '22 03:10

tanzolone