我目前正在使用Xcode 5开发一个列表导向的应用程序。我有一个自定义的标签栏色调,自定义的标签图标,自定义选中时标签栏图标的颜色,但是我找不到如何自定义未选中时图标的颜色。现在它只是默认的灰色,在我的绿色标签栏中几乎看不见。我想让标签栏图标的图片和名称变成白色。
有人知道如何在Xcode 5中设置标签栏图标的图像色调吗?
你需要为每个选项卡的(未选中的)图片设置渲染模式为UIImageRenderingModeAlwaysOriginal
。因此,在你的应用代理中,获取指向标签栏的引用,然后遍历每个标签栏项目,调整图像模式。
可能有更好的方法来获取标签栏的引用,但我做了以下操作:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tbc = [sb instantiateInitialViewController];
self.window.rootViewController = tbc;
UITabBar *tb = tbc.tabBar;
那么图像调整可以按以下步骤进行:
NSArray *items = tb.items;
for (UITabBarItem *tbi in items) {
UIImage *image = tbi.image;
tbi.selectedImage = image;
tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
你可以尝试以下方法来给所选图标着色:
// Custom the tab bar
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
并使用此方法对非活动图标进行着色:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController);
UITabBar *tabBar=tabBarController.tabBar;
UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30
}
运行并启动
设置自定义选项卡栏,包括选中和未选中的图像。同时将选项卡项图像插入位置设置为中心。
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-3.png"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-4.png"]];
item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
在第一个视图控制器的viewWillAppear方法中。
因为setFinishedSelectedImage:withFinishedUnselectedImage已经被弃用,所以我使用了Ram S的回答的修改版本,将其替换为:
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
使用:
[item0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
image
设置了渲染模式。同样地,也要为selectedImage
进行设置,尽管你可能想将它设置为不同的图像,否则你就看不出选中和未选中之间的区别了。 - Charlie Scott-Skinner