16

Apple's voice over mispronounces the title of one of my views, which is inside a UINavigation Controller.

In other parts of the app I have added a custom accessibility label to help it pronounce the company name correctly. How can I set the accessibility label of a UINavigationBar?

Warpling
  • 2,024
  • 2
  • 22
  • 38
Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40

4 Answers4

26

This works in iOS 8.2. In viewDidLoad:

self.navigationItem.accessibilityLabel = @"My accessible label";

When a navigation controller transitions to the view controller, the accessibilityLabel is read instead of the view controller title.

stevekohls
  • 2,214
  • 23
  • 29
  • Worked for me on iOS 7.1.2. This is perfect answer and should be selected as correct answer. – Vlad Jul 20 '15 at 05:55
  • 1
    Hi, I could not get this to work when a the navigation item `titleView` was set. (Works perfect without a titleView, but it defeats my purpose...) Did anybody manage to do that? Thanks. – amadour Jun 09 '16 at 14:01
  • @amadour My guess is that you will need to set the `accessibilityLabel` on your custom `titleView`. If your `titleView` has a `UILabel` inside it, you could try and set the `accessibilityLabel` on that also. – stevekohls Jun 09 '16 at 15:20
  • thanks @stevekohls, I tried that too but still could never get it to get read when the view controller is pushed. I'll comment here if I make progress on this. – amadour Jun 10 '16 at 12:32
  • It seems there is no `self.navigationItem.accessibilityIdentifier`. Why is that? – Chris Prince Oct 18 '17 at 21:40
  • 1
    @ChrisPrince `UINavigationItem` derives from `NSObject`. There is a category, `UIAccessibility`, on `NSObject` that provides `accessibilityLabel`. See `UIKit/Headers/UIAccessibility.h`. – stevekohls Oct 19 '17 at 13:20
15

I couldn't add an accessibility label, but I found a workaround:

I replace the navigationItem's title View with a UILabel that has accessibility set up.

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"myTitle";
[titleLabel setAccessibilityLabel:@"myCustomAccessiblityLabel"];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;

I'm not sure why setting the accessibility label doesn't work, but the above code works for my needs.

Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40
  • 2
    Thanks for posting your workaround! The issue with UINavigationBar might be worth filing with Apple. They are pretty good about addressing bug reports related to accessibility (including the UIBarButtonItem issue I had that I referred to above). – Turix Jul 24 '12 at 03:32
0

Since UINavigationBar inherits from UIView, you should be able to set its accessibilityLabel property. Try: yourUINavigationBar.accessibilityLabel = @"title";.

Also, you may need to ensure sure it is marked as an accessibility element with yourUINavigationBar.isAccessibilityElement = YES; (and is not inside another view which is also marked as an accessibility element). (I'm guessing this last bit may be the issue, since it appears that you already knew about accessibility labels. You can use the Accessibility Inspector in the simulator to see if this is the case by looking at the box around the element when you tap it to see if it's around something bigger than the navigator bar.)

Turix
  • 4,470
  • 2
  • 19
  • 27
  • That didn't work for me I'm afraid. I set isAccessibilityElement= YES, and the title is not inside another view that is marked as an accessibility element. – Robert Wagstaff Jul 24 '12 at 02:12
  • @RobertWagstaff Sorry! I confess I've never had this issue, but it occurs to me that maybe you need to do this for the `titleView` property of the `UINavigationItem` object instead. (BTW, this seems similar to an issue I had with the accessibility `UIBarButtonItem` in the past where it didn't behave like "normal" UIView-related stuff.) – Turix Jul 24 '12 at 02:33
  • still no luck, I tried: self.navigationItem.title = @"myTitle"; [self.navigationItem.titleView setIsAccessibilityElement:YES]; [self.navigationItem.titleView setAccessibilityLabel:@"AccessibilityTitle"]; inside of loadView – Robert Wagstaff Jul 24 '12 at 03:10
0

There a similar workaround which I have tested on iOS 11 and 12 that works as expected. For the navigationItem.titleView set a UILabel object and then set your accessibility identifier for the navigationItem.titleView.

self.navigationItem.titleView = YOUR_CUSTOM_UILABEL;

accessibilityIdentifier part

self.navigationItem.titleView.accessibilityIdentifier = YOUR_ACCESSIBILITY_IDENTIFIER;

You can see your identifier with using Xcode's Accessibility Inspector.

abdullahselek
  • 7,893
  • 3
  • 50
  • 40