【发布时间】:2025-11-25 12:15:01
【问题描述】:
我目前正在使用 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];
}
【讨论】:
image 设置渲染模式。对selectedImage 执行相同的操作,尽管您可能希望将其设置为不同的图像,否则您无法区分选中和未选中。
试试这个方法..它对我有用
应用内委托
- (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
}
奔跑吧
【讨论】:
使用选定和未选定的图像设置自定义标签栏。 还有 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: deprecated in iOS7。
【讨论】:
如果您在可视化编辑器中有标签栏,则可以在此处进行。选择选项卡栏并在“用户定义的运行时属性”中添加属性: 关键路径:selectedImageTintColor 类型:颜色 值:
【讨论】:
您可以通过添加“用户定义的运行时属性”来纯粹从情节提要中执行此操作,而无需编写任何代码:
【讨论】: