【问题标题】:Setting an accessibility label on a UITabBarItem with no title在没有标题的 UITabBarItem 上设置可访问性标签
【发布时间】:2014-12-09 16:34:46
【问题描述】:

我有一个像这样的 UITabBarItem:

_Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0];

但标题为 nil 会删除可访问性和 KIF 测试所需的标签。我发现的另一种方法是设置标题并将其移出屏幕,但这似乎是一个 hacky 解决方案:

_Controller.tabBarItem.title = @"Foo";
_Controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 200);

是否可以有一个没有标题但仍有可访问性标签的 UITabBarItem?

编辑为标签栏和背景按钮代码添加完整代码:

- (void) loadViewController {
    _Controller = [[UIViewController alloc] init];
    UIImage *normalImage = [UIImage imageNamed:@"bar.png"];
    UIImage *selectedTabImage = [UIImage imageNamed:@"barHover.png"];
    [self addCenterButtonWithImage:normalImage
                    highlightImage:selectedTabImage];

    _Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0];
}

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];

    button.center = CGPointMake(self.tabBar.frame.size.width/2.0, self.tabBar.frame.size.height/2.0 - 6.0);

    [self.tabBar addSubview:button];
}

【问题讨论】:

    标签: ios iphone ios7 kif


    【解决方案1】:

    在 iOS8 中,您可以直接为标签栏项分配辅助功能标签:

    _Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0];
    _Controller.tabBarItem.accessibilityLabel = @"Foo";
    

    对于 iOS7 及更低版本,您需要做一些事情来隐藏文本是对的。您可以像说明的那样强制它离开屏幕:

    _Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Foo" image:nil tag:0];
    _Controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 200);
    

    或者你可以让文字颜色清晰:

    _Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Foo" image:nil tag:0];
    [_Controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]} forState:UIControlStateNormal];
    

    请记住,无论您采用何种解决方案,视障用户都会使用它来导航您的应用。由于你的背景按钮是一个无法使用的装饰,你应该这样标记它:

    button.isAccessibilityElement = NO;
    button.userInteractionEnabled = NO;
    

    【讨论】:

    • 这对我不起作用。这似乎导致 UITabBarItem 后面的按钮将可访问性标签设置为其文件名(不带扩展名)而不是我设置的标签。它也仍然在 UITabBarItem 之后,因此不能被 KIF 点击。编辑:我想我应该提到 UITabBarItem 背后有一个图像。
    • 嗯...看起来accessibilityLabel在iOS8中有效,但在iOS7中无效。你如何在标签栏项目后面插入图像?您可以使用文件名作为可访问性标签来点击选项卡吗?
    • 我已将按钮的完整代码添加到原始帖子中。如果我尝试使用文件名标签,我会遇到与尝试将标签添加到 addCenterButtonWithImage 内的按钮时相同的错误:Accessibility element with label "bar" is not tappable. It may be blocked by other views. 请注意,我没有写这个,但我正在尝试使用 KIF 和即时学习。
    • AccessibilityLabel 似乎在 iOS 8 中不起作用,即使它已设置。
    • 这对我在 iOS 9 上也不起作用。 UITabBarItem 接受可访问性标签,但在应用时不会触发 VoiceOver 语音。尽管答案已被接受,但标题值似乎是用 VoiceOver 标记项目的唯一方法。清除 titleAttribute 文本颜色并没有让标题对我来说消失,所以我求助于 OP 描述的标题偏移黑客,详细信息在这里:mokagio.github.io/tech-journal/2015/02/17/…
    【解决方案2】:

    如果您尝试在 UITabBarItem 上设置 accessibilityIdentifier,除非您将 isAccessibilityElement 属性更新为 true,否则它不会显示在 Accessibility Identifier 中:

    例子:

    self.navigationController?.tabBarItem.isAccessibilityElement = true
    self.navigationController?.tabBarItem.accessibilityIdentifier = "SomeIdName"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多