在没有标题的UITabBarItem上设置辅助功能标签

时间:2014-10-14 19:00:35

标签: ios iphone ios7 kif

我有一个像这样的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];
}

2 个答案:

答案 0 :(得分:10)

在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;

答案 1 :(得分:2)

如果您尝试在UITabBarItem上设置accessibilityIdentifier,除非您将 isAccessibilityElement 属性更新为true,否则它将不会显示在辅助功能标识符中:

示例:

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