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?
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?
You can use the "User defined runtime attributes" and specify your accessibility information there:
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.
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.
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.
I got the code above to work with UIBarButtonItems with one extra line:
[view setIsAccessibilityElement:YES];
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
}
}
}
https://gist.github.com/KaneCheshire/dcce5246c3399072a5200189bfc53fe2
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.