简体   繁体   English

iOS:为什么自定义标签栏项目仅显示为灰色轮廓?

[英]iOS: Why is custom tab bar item showing only as grey silhouette?

I am trying to set the tab bar icons of my UITabBarController with custom *.png files (one for the selected and one for unselected). 我正在尝试使用自定义* .png文件设置UITabBarController的选项卡栏图标(一个用于选定文件,另一个用于未选定文件)。

The images are all in non-interlaced png format and are named correctly (@2x, @3x, etc) residing in an *.imageset. 图像全部采用非隔行png格式,并正确命名(@ 2x,@ 3x等),位于* .imageset中。

But the tab bar items are only shown as silhouettes like this: 但是选项卡栏项仅显示为这样的轮廓:

在此处输入图片说明

I tried to set these images within Interface Builder, without success. 我试图在Interface Builder中设置这些图像,但没有成功。 Then I also tried to set them programmatically in the "loadView" function of MyTabBarController (which is extending UITabBarController) like this: 然后,我还尝试以编程方式在MyTabBarController的“ loadView”函数中设置它们(正在扩展UITabBarController),如下所示:

UIImage *selectedImage;
UIImage *unselectedImage;

// tab1
selectedImage = [UIImage imageNamed:@"cmdGamesActive"];
unselectedImage = [UIImage imageNamed:@"cmdGamesInactive"];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:0];
item1 = [item1 initWithTitle:@"Games" image:unselectedImage selectedImage:selectedImage];

// tab2
selectedImage = [UIImage imageNamed:@"cmdFriendsActive"];
unselectedImage = [UIImage imageNamed:@"cmdFriendsInactive"];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:1];
item2 = [item2 initWithTitle:@"Friends" image:unselectedImage selectedImage:selectedImage];

// tab3
selectedImage = [UIImage imageNamed:@"cmdTrophiesActive"];
unselectedImage = [UIImage imageNamed:@"cmdTrophiesInactive"];
UITabBarItem *item3 = [self.tabBar.items objectAtIndex:2];
item3 = [item3 initWithTitle:@"Trophies" image:unselectedImage selectedImage:selectedImage];

// tab4
selectedImage = [UIImage imageNamed:@"cmdSettingsActive"];
unselectedImage = [UIImage imageNamed:@"cmdSettingsInactive"];
UITabBarItem *item4 = [self.tabBar.items objectAtIndex:3];
item4 = [item4 initWithTitle:@"Settings" image:unselectedImage selectedImage:selectedImage];

... with the same result. ...具有相同的结果。

Any ideas how to solve this issue ? 任何想法如何解决这个问题?

According to this answer , I did something extra and kind of have answer for you here. 根据此答案 ,我在这里为您做了一些额外的工作。 I have my custom UITabBarController, which is linked with my UITabBarController in the StoryBoard file. 我有我的自定义UITabBarController,它已与StoryBoard文件中的UITabBarController链接。 So in order to remove the automatic tint provided by iOS when the TabBar is unselected, I ended up removing it in this manner. 因此,为了在未选中TabBar时删除iOS提供的自动色调,我最终以这种方式删除了它。 The images can be a vast variety of images but just in the way recommended here . 这些图像可以是各种各样的图像,但只是此处建议的方式。 Here it goes: 它去了:

NSArray *navConArr = self.viewControllers;//self is custom UITabBarController
UINavigationController *naviOne = [navConArr objectAtIndex:0];//I have 3 different tabs, objectAtIndex:0 means the first tab navigation controller
UITabBarItem *naviBtn  = naviOne.tabBarItem;
UIImage *image = [[UIImage imageNamed:@"iconNaviOne"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[naviBtn setSelectedImage:image];
[naviBtn setImage:image];

Thankfully, this works like a charm (: 值得庆幸的是,这就像一个魅力(:

I was facing a similar issue. 我面临着类似的问题。 I had chosen few icons for my tabs such as 'mic', 'smiley' and 'settings'. 我为标签选择了一些图标,例如“麦克风”,“笑脸”和“设置”。

But the only things visible when I executed the program were grey and blue squares. 但是我执行程序时唯一可见的是灰色和蓝色方块。

I modified my '.png' images to be transparent and now they are visible as they were intended to. 我将“ .png”图像修改为透明,现在可以按预期显示它们了。

ie, make sure the images used for tab icons do not have a background. 即,确保用于选项卡图标的图像没有背景。

This should do the trick: 这应该可以解决问题:

UITabBarItem *item0 = [_tabBar.items objectAtIndex:0];
item0.image  = [[UIImage imageNamed:@"edit_profile_tab"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item0.selectedImage  = [[UIImage imageNamed:@"edit_profile_tab_active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM