ios 在 UIBarButtonItem 上以编程方式设置可访问性标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20543804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:45:05  来源:igfitidea点击:

Setting accessibility identifier programmatically on UIBarButtonItem

iosobjective-cautomationaccessibility

提问by Chris

The accessibility identifier is a developer generated ID for GUI objects, which can be used for automation tests.

可访问性标识符是开发人员为 GUI 对象生成的 ID,可用于自动化测试。

A UIBarButtonItemdoes not implement UIAccessibilityIdentification. However is there a possibility that I can assign an accessibility identifier?

AUIBarButtonItem没有实现UIAccessibilityIdentification。但是,我可以分配可访问性标识符吗?

采纳答案by Don Miguel

You could subclass UIBarButtonItemand implement the UIAccessibilityIdentificationprotocol in that subclass, lets's say BarButtonWithAccesibility.

您可以子类化UIBarButtonItemUIAccessibilityIdentification在该子类中实现协议,例如BarButtonWithAccesibility.

In BarButtonWithAccesibility.h:

BarButtonWithAccesibility.h

@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification>

@property(nonatomic, copy) NSString *accessibilityIdentifier NS_AVAILABLE_IOS(5_0);

The only (strict) requirement for adhering to this protocol is defining the accessibilityIdentifierproperty.

遵守此协议的唯一(严格)要求是定义accessibilityIdentifier属性。

Now in your view controller, let's say in viewDidLoad, you could set up a UIToolbar and add your subclassed UIBarButtonItem:

现在在你的视图控制器中,假设viewDidLoad你可以设置一个 UIToolbar 并添加你的子类 UIBarButtonItem:

#import "BarButtonWithAccesibility.h"

- (void)viewDidLoad{

    [super viewDidLoad];

    UIToolbar *toolbar = [[UIToolbar alloc]  initWithFrame:CGRectMake(0, 0, 320, 44)];

    BarButtonWithAccesibility *myBarButton = [[BarButtonWithAccesibility alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonPressed:)];
    myBarButton.accessibilityIdentifier = @"I am a test button!";

    toolbar.items = [[NSArray alloc] initWithObjects:myBarButton, nil];
    [self.view addSubview:toolbar];
}

And within the buttonPressed:you could verify that you have access to the accessibilityIdentifier:

buttonPressed:您可以在其中验证您是否有权访问accessibilityIdentifier

- (void)buttonPressed:(id)sender{
    if ([sender isKindOfClass:[BarButtonWithAccesibility class]]) {
        BarButtonWithAccesibility *theButton = (BarButtonWithAccesibility *)sender;
        NSLog(@"My accesibility identifier is: %@", theButton.accessibilityIdentifier);
    }
}

Hope this helps.

希望这可以帮助。

回答by Stas Zhukovskiy

As of iOS 5 you can do it like this:

从 iOS 5 开始,您可以这样做:

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

回答by Hussain Shabbir

If you have UIToolbarcreated inside that if you want to create multiple UIBarButtonItemprogramatically then it can be accessed like that and set accessibilityLabelas well like that below:-

如果你已经UIToolbar在里面创建,如果你想以UIBarButtonItem编程方式创建多个,那么它可以accessibilityLabel像下面那样访问和设置:-

    -(void)viewDidAppear:(BOOL)animated
    {
        UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];
        [self.customToolBar setItems:[NSArray arrayWithObject:infoButtonItem]];
//Here if you have muliple you can loop through it 
       UIView *view = (UIView*)[self.customToolBar.items objectAtIndex:0];
    [view setAccessibilityLabel:NSLocalizedString(@"Test", @"")];
    }

回答by Ben Boral

The subclassing UIBarButtonItemis a good solution. Depending on your needs, though, it may make more sense to simply assign the accessibilityIdentifierto the custom image of your UIBarButtonItem, assuming your UIBarButtonItemuses a custom image.

子类化UIBarButtonItem是一个很好的解决方案。但是,根据您的需要,假设您使用自定义图像,简单地将 分配accessibilityIdentifier给 的自定义图像可能更有意义。UIBarButtonItemUIBarButtonItem