我有一个像这样的UITabBarItem:
_Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:nil tag:0];
但是没有标题会删除可访问性和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]; }
解决方法
在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;