标签栏项目的自定义图像

Posted

技术标签:

【中文标题】标签栏项目的自定义图像【英文标题】:custom image for tabbar item 【发布时间】:2016-08-09 11:33:52 【问题描述】:

更改 UITabBar 项的自定义图像 我用过这段代码,但它不起作用。

UIImage *selectedImage0 = [UIImage imageNamed:@"f.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"f.png"];

UIImage *selectedImage1 = [UIImage imageNamed:@"g.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:@"g.png"];

UIImage *selectedImage2 = [UIImage imageNamed:@"i.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:@"i.png"];

UIImage *selectedImage3 = [UIImage imageNamed:@"s.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:@"s.png"];

UIImage *selectedImage4 = [UIImage imageNamed:@"t.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:@"t.png"];


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];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];

[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];

【问题讨论】:

【参考方案1】:

您应该使用- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_ios(7_0); 方法,如下所示

item0.image = [unselectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item0.selectedImage = [selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

【讨论】:

【参考方案2】:

您已为每个 tabitem 简单图像和悬停图像设置了两个图像。

item0.image = [[UIImage imageNamed:@"f.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // set simple image

item0.selectedImage = [[UIImage imageNamed:@"f.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // set hover image

【讨论】:

不。我有自己的答案,但上传答案时出现问题。在更新我的答案之前你已经回答了..

以上是关于标签栏项目的自定义图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在ios中更改标签栏项目的背景图像?

swift和xcode 6中的自定义标签栏

标签栏项目图像不显示 - xcode 6 [关闭]

MonoTouch 中的自定义标签栏项目

我怎么能创建一个类似于这个的自定义标签栏

如何让自定义标签栏显示标签栏项目在 Xcode 中设置的选定图像?