我目前使用 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
}
跑走
如果你在可视化编辑器中有你的标签栏,你可以在这里做。选择标签栏并在“用户定义的运行时属性”中添加属性:键路径:selectedImageTintColor 类型:颜色值:
使用选定和未选定的图像设置自定义标签栏。还有 tabbarItem Image Insets 位置在中心
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]];
有关详细信息,请参阅UITabBarItem setFinishedSelectedImage:在 iOS7 中已弃用。