UITabBarController选定的项目

3

我有一个UITabBarController,并且在两个视图上进行推送。我希望第一个视图是默认的,并且在加载时按钮处于“选中”状态。

我的问题是tabBarItem在加载时没有选中的背景。我必须点击它才能显示选中状态。我还使用自定义图像作为标签栏项,并在添加到tabBarController的每个视图中进行配置。

我使用以下代码来设置选定的项:

_tabBarController.selectedViewController = [_tabBarController.viewControllers objectAtIndex:0];

UITabBarItem会在我添加到tabBarController的每个视图中创建:

-(UITabBarItem *)tabBarItem
{
    return [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"tab_select_indicator_map.png"]  tag:0];
}

我该如何使选项卡项目显示已选状态?

您是否解决了您的问题?我正在推送一个TabBarController,但初始选项卡图标未被选中(尽管ViewController已正确显示)。只有在实际的选项卡图标上点击才会选中它。而且我是通过编程方式设置selectedIndex的。 - Thomas Krajacic
1个回答

1
当您的选项卡栏加载时,请使用以下代码显示所选选项卡项。
[_tabBarController setSelectedIndex:0]; // your can set index as per your requirement.

编辑:

请确保您创建的tabBarItem符合以下格式

UITabBarItem* myTestingItem = [[UITabBarItem alloc] init];
[myTestingItem  setFinishedSelectedImage: [UIImage imageNamed: @"btnTabItem-SelectedImage.png"]
              withFinishedUnselectedImage: [UIImage imageNamed: @"btnTabItem.png"]];

但它并没有给tabBarItem设置“selected”状态。我仍然需要点击才能显示该状态。 - Steve Sloka
我更新了问题以展示如何创建tabBarItem。现在我没有设置未选中图像,但当我点击时会得到一个。 - Steve Sloka
我没有这个图片 "btnTabItem-SelectedImage.png",iOS 在我点击时会自动在项目周围放置“选中”的背景。 - Steve Sloka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接