Setting an accessibility label on a UITabBarItem without a header

I have a UITabBarItem, for example:

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

But having zero for the header removes the shortcut needed for accessibility and testing KIF. The alternative I found is to set the title and move it from the screen, but this seems like a hacker solution:

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

Is it possible to have a UITabBarItem without a header, but there is still an accessibility label?

EDIT to add full code for tab bar and background button code:

 - (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]; } 
+6
source share
2 answers

In iOS8, you can assign an accessibility label directly to a tab bar item:

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

For iOS7 and below, you are correct that you need to do something to hide the text. You can force it to be disabled, as shown in the figure:

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

Or you can make the text color understandable:

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

Remember that any solution that you come will be used by visually impaired users to navigate your application. Since your background button is unfit for decoration, you should mark it as such:

 button.isAccessibilityElement = NO; button.userInteractionEnabled = NO; 
+10
source

If you try to set the accessibilityIdentifier to UITabBarItem, it will not appear in the availability identifier unless you change the isAccessibilityElement property to true:

Example:

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

Source: https://habr.com/ru/post/976739/


All Articles