31

I have several UIBarButtonItem objects in Interface Builder, and I cannot find any option to set the accessibility label or hint for these buttons.

How can I set these attributes?

Zorayr
  • 23,770
  • 8
  • 136
  • 129
Tim Norman
  • 1,999
  • 1
  • 20
  • 26

8 Answers8

28

You can use the "User defined runtime attributes" and specify your accessibility information there: Runtime attributes

Rafael Nobre
  • 5,062
  • 40
  • 40
24

It is not actually enough to just set the accessibilityLabel User Defined Runtime Attribute in Interface Builder. You also have to set isAccessibilityElement to true. This keeps all of the accessibility information within Interface Builder.

IB Screenshot

shim
  • 9,289
  • 12
  • 69
  • 108
Dan
  • 367
  • 3
  • 7
15

Okay, so it seems there's no way to do it in Interface Builder, even though you can set accessibility attributes on other UI elements using IB. So I set a tag on my toolbar and then added this code to my viewWillAppear method:

UIToolbar *bottombar = (UIToolbar*)[self viewWithTag:kBottomToolbar];

UIView *view = (UIView*)[bottombar.items objectAtIndex:0];
[view setAccessibilityLabel:NSLocalizedString(@"Add Bookmark", @"")];
[view setAccessibilityHint:NSLocalizedString(@"Add Bookmark", @"")];

and so on for each button item...

Not the most elegant, but it works.

Tim Norman
  • 1,999
  • 1
  • 20
  • 26
  • 2
    As of iOS 5.0 you can directly set the `accessibilityLabel` property on a `UIBarButtonItem`. – rmaddy Mar 11 '13 at 03:39
7

I know this is old but I've just run into a need for this. As of iOS 5.0, you can now easily set the accessibility label of a UIBarButtonItem by doing:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] init...;
btn.accessibilityLabel = @"Label";

No more hacks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
4

I got the code above to work with UIBarButtonItems with one extra line:

[view setIsAccessibilityElement:YES];
shim
  • 9,289
  • 12
  • 69
  • 108
  • This resolved the issue. Keep in mind that if a parent view also has this set to YES, then the subviews are ignored. – Aaron Brager Mar 13 '13 at 19:11
3

You can use IBInspectable for this so you get some handy options in Interface Builder's side panel.

public extension UIBarButtonItem {
  @IBInspectable var accessibilityEnabled: Bool {
    get {
      return isAccessibilityElement
    }
    set {
      isAccessibilityElement = newValue
    }
  }

  @IBInspectable var accessibilityLabelText: String? {
    get {
      return accessibilityLabel
    }
    set {
      accessibilityLabel = newValue
    }
  }
}

UIBarButton Accessibility options in Interface Builder

https://gist.github.com/KaneCheshire/dcce5246c3399072a5200189bfc53fe2

Kane Cheshire
  • 1,654
  • 17
  • 20
2

Trying to set accessibility labels manually didn't work for me with UIBarButtonItem images. However, if I set the title manually then the label would work. But it displays the title below the image.

I ended up creating an UIButton and using it as a custom view for the UIBarButtonItem. The only loss being the image masking that UIBarButtonItem performs. Added bonus: accessibility configurable in IB.

1

In Xcode 10.2, the Title field works well. Here, for example, my button is accessible as "Sort":

enter image description here

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118