How to set accessibility label on an iOS UIBarButtonItem

6,308 views
Skip to first unread message

Joel Rosi-Schwartz

unread,
Jan 2, 2013, 12:03:37 PM1/2/13
to kif-fr...@googlegroups.com

I need to set the accessibility label on my navigation bar back button. I have tried this but does not seem to take effect.

[self.navigationItem.backBarButtonItem setAccessibilityLabel:NSLocalizedString(kNavBarBackButtonAccessibilityLabel, @"")];
[self.navigationItem.backBarButtonItem setAccessibilityHint:NSLocalizedString(kNavBarBackButtonAccessibilityLabel, @"")];
[self.navigationItem.backBarButtonItem setIsAccessibilityElement:YES];

I set up my test with

    + (id) stepToGoBackToExplorations {
        return [KIFTestStepstepToTapViewWithAccessibilityLabel:kNavBarBackButtonAccessibilityLabel];

    }

I get back

FAIL (10.01s): Tap view with accessibility label "Back button"

2013-01-02 16:41:02.771 KIF Functional Testing[60766:c07] FAILING ERROR: Error Domain=KIFTest Code=0 "The step timed out after 10.00 seconds." UserInfo=0xa6494a0 {NSUnderlyingError=0xa6509a0 "Failed to find accessibility element with the label "Back button"", NSLocalizedDescription=The step timed out after 10.00 seconds.}

I have also tried this as suggested by Set the accessibility attributes on UIBarButtonItem 4 

UIView *view = (UIView*)self.navigationItem.leftBarButtonItem;
[view setAccessibilityLabel:NSLocalizedString(kNavBarBackButtonAccessibilityLabel, @"")];
[view setAccessibilityHint:NSLocalizedString(kNavBarBackButtonAccessibilityLabel, @"")];

After Googling this extensively I have come up blank with how to accomplish this.

Advice would be much appreciated.

Thanks,

Joel

Joel Rosi-Schwartz

unread,
Jan 2, 2013, 12:19:39 PM1/2/13
to kif-fr...@googlegroups.com
I have modified to my step to as below. This works and but I would still be interested in figuring out why I am not able to set up the accessibility label.

+ (id) stepToGoBackToExplorations {

    // return [KIFTestStep stepToTapViewWithAccessibilityLabel:kNavBarBackButtonAccessibilityLabel];

    return [KIFTestStep stepWithDescription:@"Select the Details text view." executionBlock: ^(KIFTestStep * step, NSError * *error) {

        TITestUtility *testUtil = [TITestUtility sharedUtility];

        UINavigationController * navigationController = testUtil.navigationController;

        [navigationController popToRootViewControllerAnimated:YES];

        return KIFTestStepResultSuccess;

    }

            ];

}

hung...@stanyangroup.com

unread,
Jan 8, 2013, 4:48:20 AM1/8/13
to kif-fr...@googlegroups.com
I am pretty sure the back button has the label of "Back". You can't override the accessibilityLabel either.

One trick to find out the default value for accessibility label for an element is to use the accessibility inspector in the simulator. You enable this in Settings. Extremely helpful.

Joel Rosi-Schwartz

unread,
Jan 8, 2013, 11:06:56 AM1/8/13
to kif-fr...@googlegroups.com
Is this what you mean?

[scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:@"Back"]];

That does not seem to work for me it blocks and fails.

hung...@stanyangroup.com

unread,
Jan 8, 2013, 11:04:15 PM1/8/13
to kif-fr...@googlegroups.com
Try to use the Accessibility Inspector? It'll show you what the label is.

Shannen Lambdin

unread,
Jun 6, 2014, 3:24:19 PM6/6/14
to kif-fr...@googlegroups.com
I'm having a similar problem. I've tried using the accessibility inspector and it shows that there is no label even though i programmatically set it and stepping through the code I can see that it gets set.
Reply all
Reply to author
Forward
0 new messages