Xcode动态添加标签栏图标

时间:2012-09-25 19:26:58

标签: xcode icons uitabbarcontroller uitabbar uitabbaritem

我已经阅读了有关在app delegate和界面构建器中添加tabbar图标的不同答案。我想在我的firtsviewcontroller中添加它们,因为我想从plist中读取图标图像名称。 建议这样做:

NSArray *viewControllerArray = nil;
viewController1 = <View Init Code>
viewController2 = <View Init Code>
viewController3 = <View Init Code>

1stNavController = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
UIImage *img = [UIImage imageNamed:@"tab_home"];
[1stNavController .tabBarItem initWithTitle:@"Home" image:img tag:1];

但我不喜欢这样做,因为我觉得这不干净。我想做类似以下的事情,但我不知道它为什么不起作用:

[[self.tabBarController.tabBar.items objectAtIndex:2] setIcon:[UIImage imageNamed:....]];

任何解决方案?

1 个答案:

答案 0 :(得分:0)

如下:

NSArray *viewControllers = self.tabBarController.viewControllers;
((UIViewController*)viewControllers[0]).tabBarItem.image = [UIImage imageNamed:...];
((UIViewController*)viewControllers[1]).tabBarItem.image = [UIImage imageNamed:...];
((UIViewController*)viewControllers[2]).tabBarItem.image = [UIImage imageNamed:...];
((UIViewController*)viewControllers[3]).tabBarItem.image = [UIImage imageNamed:...];
((UIViewController*)viewControllers[4]).tabBarItem.image = [UIImage imageNamed:...];