我正在使用带有导航 Controller 的基于tabBar
的应用程序。在我的应用程序中,我想更改 tabBarItem image
和 title
。单击特定的 tabBarItem
时,控件将转到该特定 View 的 viewWillAppear
方法。这次我想更改该特定 tabBarItem
的 image
和 title
。
我是这样给予的,但是,它不起作用......
代码:
tabItem = [[UITabBarItem alloc] initWithTitle:@"Colors" image:[UIImage imageNamed:@"Colors.png"] tag:9];
self.tabBarController.tabBarItem = tabItem;
我已经将该 View 的标记值
设置为9
。
请帮助我如何更改该特定 tabbarItem
的 image
和 title
。
最佳答案
您尝试做的事情存在轻微的误解。首先, UITabBarController 没有 tabBarItem 属性,这正是您尝试设置的属性。但是,即使确实如此,UITabBarController 文档也特别指出,关于 UITabBarController 的 tabBar 属性:
You should never attempt to manipulate the UITabBar object itself stored in this property. If you attempt to do so, the tab bar view throws an exception.
因此,修改项目的方式不是通过UITabBar本身。在您的情况下,UITabBar 正在从每个选项卡表示的 UIViewControllers 获取其 UITabBarItems。这是你可以控制的。具体来说,您可以做的是:
- (void)viewWillAppear:(BOOL)animated {
/* Modifies the UITabBarItem used by a UITabBarController to represent this
* UIViewController in the tab bar.
*/
UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Colors" image:[UIImage imageNamed:@"Colors.png"] tag:9];
self.tabBarItem = tabBarItem;
[tabBarItem release];
}
关于iphone - 如何更改 TabBarItem 图像和标题问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4721669/