使用TabBarViewController更改UITabBar的图标,而TabBarViewController不是rootViewController

时间:2014-11-25 17:37:58

标签: ios storyboard uitabbar

我想更改我的UITabBar的图标。是的,StackOverFlow上有几个主题,我读了很多,但我找不到答案。

我的第一个View Controller是一个Login View Controller和  我的第二个VC是TabBarViewController。我将TheBabBar命名为StoryBoard中的TabBarViewController。

这是我的代码,但它没有用,我不知道原因:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController* tabBarController = (UITabBarController*)[storyboard instantiateViewControllerWithIdentifier:@"TheTabBar"];

NSLog(@"STORY = %@",storyboard);
NSLog(@"TABBAR = %@",tabBarController);

UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];

tabBarItem1.selectedImage = [[UIImage imageNamed:@"firstselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"firstnonselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = @"First";

tabBarItem2.selectedImage = [[UIImage imageNamed:@"secondselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"secondnonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = @"Second";

tabBarItem3.selectedImage = [[UIImage imageNamed:@"thirdselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"thirdnonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = @"Third";

tabBarItem4.selectedImage = [[UIImage imageNamed:@"fourselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"fournonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = @"Four";

tabBarItem5.selectedImage = [[UIImage imageNamed:@"fiveselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem5.image = [[UIImage imageNamed:@"fivenonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem5.title = @"Five";

谢谢大家!

2 个答案:

答案 0 :(得分:4)

如果您使用StoryBoard,则可以使用“用户定义的运行时属性”来更改UITabBarItem的所选图像。

它看起来像这样: enter image description here

“选定的图像”属性Inspector中的属性不起作用,这是Xcode的错误。

您可以使用图像集设置图像的RenderingMode(渲染为): enter image description here

答案 1 :(得分:0)

您可以在申请的UITabBar文件中声明AppDelegate。然后将控制器中的标签栏分配给AppDelegate的标签栏对象。然后,您可以使用来自任何控制器的app delegate的tabbar对象更改tabbar的图标。