我目前正在使用Xcode 5来开发面向列表的应用程序。我有标签栏的自定义色调,标签图标的自定义图像,选中标签栏的图标图像的自定义色调,但我找不到如何自定义图标图像的未选中时的色调。现在它只是默认的灰色,与我的绿色标签栏相比,你几乎看不到它。我想将标签栏图标的图像和名称设为白色。
有人知道如何在Xcode 5中设置标签栏图标的图像色调吗?
答案 0 :(得分:26)
您需要将每个标签(未选中)图像的渲染模式设置为UIImageRenderingModeAlwaysOriginal
。因此,在您的app委托中,获取对标签栏的引用,然后遍历每个标签栏项目,调整图像模式。
可能有更好的方法来获得对标签栏的引用,但我做了以下内容:
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];
}
答案 1 :(得分:17)
您可以尝试使用此颜色选择图标:
// Custom the tab bar
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
这是为非活动图标着色:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
答案 2 :(得分:6)
您可以通过添加"用户定义的运行时属性"来完全从故事板中完成此操作而无需编写任何代码:
答案 3 :(得分:5)
试试这种方式..这对我有用
在app delegate中
- (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
}
跑去吧
答案 4 :(得分:5)
如果您在可视化编辑器中有标签栏,则可以在此处执行此操作。选择选项卡栏并在“用户定义的运行时属性”中添加属性: 关键路径:选择图像色调颜色 类型:颜色 价值:
答案 5 :(得分:4)
使用选定和非选择的图像设置自定义标签栏。 还有tabbarItem图像插入位置在中心
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);
**在第一个viewcontroller的viewWillAppear方法中。 **
答案 6 :(得分:3)
因为不推荐使用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]];
有关详细信息,请参阅UITabBarItem setFinishedSelectedImage: deprecated in iOS7。