当我在程序中选择这样做时,我想知道如何使 UITabBarItem 突出显示。这可能吗?
谢谢
您可以轻松更改选择的选项卡 - 这也更改了突出显示:
tabcontroller.selectedIndex = youNewIndexHere;
如果您的选项卡控制器在应用程序委托中(如果您在创建项目时使用选项卡栏应用程序模板就是这种情况),它将如下所示:
ProjectNameAppDelegate *appDelegate = (ProjectNameAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tabcontroller].selectedIndex = youNewIndexHere;
如果您知道索引并像这样编写它,tabbar 就可以工作:
NSMutableArray *item [[NSMutableArray alloc] init];
[items addObject: [[[UITabBarItem alloc] initAsULike] autorelease]; <--- init as you like here
...
[tabBar setItems:items animated:FALSE];
tabBar.selectedItem = [items objectAtIndex: your_index]; <--- write your index here
[items release];
我有一个在 IB 中创建但没有 TabBarController 的 UITabBar。在我的 awakeFromNib 中,我只是将它初始化为第一个项目,如下所示:
[self.tabBar setSelectedItem:[[self.tabBar items] objectAtIndex:0]];
不要忘记,在这种情况下不会调用 UITabBarDelegate didSelectItem。