UITabBar自定义图标颜色未显示

时间:2015-01-09 09:45:39

标签: ios ios7 uitabbar

我在iOS应用中使用UITabBar。我已将图像设置为UITabBar项目。代码是 -

self.mainTabBar.delegate = self;
mainTabBar.tintColor = nil;

[[mainTabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"ic_1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ic_1.png"]];
[[mainTabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"ic_2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ic_2.png"]];
[[mainTabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@"ic_3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ic_3.png"]];
[[mainTabBar.items objectAtIndex:3] setFinishedSelectedImage:[UIImage imageNamed:@"ic_4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ic_4.png"]];
[[mainTabBar.items objectAtIndex:4] setFinishedSelectedImage:[UIImage imageNamed:@"ic_5.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ic_5.png"]];

这是标签栏的代码。它显示像 -  enter image description here 实际图标图像就像(颜色是这样的) - enter image description here

如何显示图标的实际颜色?

3 个答案:

答案 0 :(得分:3)

如果您使用的是iOS 8,则可以像这样使用它。

UIImage *image1 = [[UIImage imageNamed:@"message"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

tabBarItem1.image = image1;
tabBarItem1.selectedImage = image11;

以下是来源:UITabBarItem/selectedImage

  

默认情况下,将自动创建实际选定的图像   源图像中的alpha值。为了防止系统着色,   使用UIImageRenderingModeAlwaysOriginal提供图像。

答案 1 :(得分:0)

从视图控制器中设置UITabBarItem s。不要直接设置它们。

例如:

UITableViewController *searchTableCon = [[UITableViewController alloc]init];
UINavigationController *searchNavCon = [[UINavigationController alloc]initWithRootViewController:searchTableCon];

searchTableCon.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Search"
                                                    image:[UIImage imageNamed:@"Search"]
                                            selectedImage:[UIImage imageNamed:@"Search"]];

答案 2 :(得分:0)

你必须做以下事情。

UIImage * selectedImage = [UIImage imageNamed:@"selected_image.png"];
selectedImage =    [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewCtrl.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Title" image:selectedImage selectedImage:selectedImage];