Tim Norman
Tim Norman

Reputation: 2009

Set the accessibility attributes on UIBarButtonItem

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?

Upvotes: 32

Views: 18877

Answers (8)

Dan
Dan

Reputation: 367

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

Upvotes: 24

Lee Ann Rucker
Lee Ann Rucker

Reputation: 81

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

[view setIsAccessibilityElement:YES];

Upvotes: 4

Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

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

enter image description here

Upvotes: 1

Kane Cheshire
Kane Cheshire

Reputation: 1712

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

Upvotes: 3

Rafael Nobre
Rafael Nobre

Reputation: 5131

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

Upvotes: 28

rmaddy
rmaddy

Reputation: 318794

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.

Upvotes: 7

user243578
user243578

Reputation:

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.

Upvotes: 2

Tim Norman
Tim Norman

Reputation: 2009

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.

Upvotes: 15

Related Questions