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