Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting accessibility identifier programmatically on UIBarButtonItem

The accessibility identifier is a developer generated ID for GUI objects, which can be used for automation tests.

A UIBarButtonItem does not implement UIAccessibilityIdentification. However is there a possibility that I can assign an accessibility identifier?

like image 790
Chris Avatar asked Dec 12 '13 12:12

Chris


3 Answers

You could subclass UIBarButtonItem and implement the UIAccessibilityIdentification protocol in that subclass, lets's say BarButtonWithAccesibility.

In BarButtonWithAccesibility.h:

@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification>  @property(nonatomic, copy) NSString *accessibilityIdentifier NS_AVAILABLE_IOS(5_0); 

The only (strict) requirement for adhering to this protocol is defining the accessibilityIdentifier property.

Now in your view controller, let's say in viewDidLoad, you could set up a UIToolbar and add your subclassed UIBarButtonItem:

#import "BarButtonWithAccesibility.h"  - (void)viewDidLoad{      [super viewDidLoad];      UIToolbar *toolbar = [[UIToolbar alloc]  initWithFrame:CGRectMake(0, 0, 320, 44)];      BarButtonWithAccesibility *myBarButton = [[BarButtonWithAccesibility alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonPressed:)];     myBarButton.accessibilityIdentifier = @"I am a test button!";      toolbar.items = [[NSArray alloc] initWithObjects:myBarButton, nil];     [self.view addSubview:toolbar]; } 

And within the buttonPressed: you could verify that you have access to the accessibilityIdentifier:

- (void)buttonPressed:(id)sender{     if ([sender isKindOfClass:[BarButtonWithAccesibility class]]) {         BarButtonWithAccesibility *theButton = (BarButtonWithAccesibility *)sender;         NSLog(@"My accesibility identifier is: %@", theButton.accessibilityIdentifier);     } } 

Hope this helps.

like image 102
Don Miguel Avatar answered Sep 25 '22 05:09

Don Miguel


As of iOS 5 you can do it like this:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] init...;
btn.accessibilityLabel = @"Label";
like image 34
Stas Zhukovskiy Avatar answered Sep 25 '22 05:09

Stas Zhukovskiy


If you have UIToolbar created inside that if you want to create multiple UIBarButtonItem programatically then it can be accessed like that and set accessibilityLabel as well like that below:-

    -(void)viewDidAppear:(BOOL)animated
    {
        UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];
        [self.customToolBar setItems:[NSArray arrayWithObject:infoButtonItem]];
//Here if you have muliple you can loop through it 
       UIView *view = (UIView*)[self.customToolBar.items objectAtIndex:0];
    [view setAccessibilityLabel:NSLocalizedString(@"Test", @"")];
    }
like image 38
Hussain Shabbir Avatar answered Sep 23 '22 05:09

Hussain Shabbir