我目前正在使用Xcode 5开发一个面向列表的应用程序。我有一个自定义色彩的标签栏,自定义图像的选项卡图标,自定义色彩选项卡栏的图标图像时,但我找不到如何自定义图标图像的颜色,当它没有被选中。现在它只是默认的灰色,你几乎看不到我的绿色标签栏。我想让标签栏图标的图像和名称为白色。自定义标签栏图标颜色
有人知道如何在Xcode 5中设置标签栏图标的图像色调?
我目前正在使用Xcode 5开发一个面向列表的应用程序。我有一个自定义色彩的标签栏,自定义图像的选项卡图标,自定义色彩选项卡栏的图标图像时,但我找不到如何自定义图标图像的颜色,当它没有被选中。现在它只是默认的灰色,你几乎看不到我的绿色标签栏。我想让标签栏图标的图像和名称为白色。自定义标签栏图标颜色
有人知道如何在Xcode 5中设置标签栏图标的图像色调?
你可以试试这个以色彩选择的图标:
// Custom the tab bar
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
,这对着色非活动图标:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
你需要为每个选项卡的(未选)图像的渲染模式到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];
}
试试这个way..it工作对我来说
在应用程序委托
- (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中方法tabbarItem图片插图位置。 **
因为setFinishedSelectedImage:
[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 withFinishedUnselectedImage已过时,我通过更换使用拉姆小号的答案的修改过的版本。
谢谢,你救了我两天... –
如果你在可视化编辑器中有你的标签栏,你可以在这里完成。选择标签栏,并在“用户自定义运行属性”添加属性: 关键路径:selectedImageTintColor 类型:彩色 值:
就像一个魅力。对于未选定的图标是否还有一个美妙的魔力? –
你可以这样做纯粹是从故事板没有通过添加“用户自定义运行属性编写任何代码“:
很高兴知道。但用户定义的运行时属性是绝对的调试噩梦。 –
除了tintColor,你还可以在Key Path中输入什么内容? –
@RasmusBidstrup,所选IB项目的所有相关属性。在我们的例子中是UITabBar。所以去苹果文档并阅读UITabBar文档。 – OhadM
注意setFinisheSelectedImage:withFinishedUnselectedImage在iOS的7 –
马修弃用如果在iOS的7弃用有另一种方式去这还是是,仅仅因为苹果希望保持灰色默认图标的标准呢? – ian
@ user2792129 - 是的,只是花了我几分钟让我的答案输入 –