问题描述
图标在ios 6中显示正常但在ios 7中没有显示。我在viewController viewDidLoad方法中设置了所选状态。当用户选择标签栏项目时,图像消失。
这是我的代码:
The icons show fine in ios 6 but not in ios 7. I'm setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disappears.Here is my code:
UITabBar *tabBar = self.tabBarController.tabBar;
if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)]) {
[self.tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
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 setTitle:@"Home"];
[item1 setTitle:@"Calendar"];
[item2 setTitle:@"News"];
[item3 setTitle:@"My Events"];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"homeIconSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"Calendar"] withFinishedUnselectedImage:[UIImage imageNamed:@"CalendarIconSelected"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"NewsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"News"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"EventsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"Events"]];
[item1 imageInsets];
[item2 imageInsets];
[item3 imageInsets];
推荐答案
您需要使用tabBarItem initWithTitle:image:selectedImage
You need to use tabBarItem initWithTitle:image:selectedImage
[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];
与更改UIImage呈现模式相结合:
in conjunction with changing the UIImage rendering mode:
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
或(以应用父视图模板色调掩码,此选项是标签栏项目的默认选项,除非您选择退出上述渲染模式)
or (to apply parent views template tint mask, this option is default for Tab bar Items unless you opt out with the above rendering mode)
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
这是一个标签栏项目的代码示例: -
here is a code sample for one tab bar item :-
UIImage *musicImage = [UIImage imageNamed:@"music.png"];
UIImage *musicImageSel = [UIImage imageNamed:@"musicSel.png"];
musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image:musicImage selectedImage:musicImageSel];
希望这会有所帮助
这篇关于UITabBar未在ios 7中显示所选项目图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!