objective-c UITabBar 未在 ios 7 中显示所选项目图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18894985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITabBar not showing selected item images in ios 7
提问by user1898829
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:
图标在 ios 6 中显示正常,但在 ios 7 中显示不正常。我在 viewController viewDidLoad 方法中设置选定状态。当用户选择标签栏项目时,图像消失。这是我的代码:
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];
回答by gav
You need to use tabBarItem initWithTitle:image:selectedImage
您需要使用 tabBarItem initWithTitle:image:selectedImage
[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];
in conjunction with changing the UIImage rendering mode:
结合改变 UIImage 渲染模式:
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];
Hope this helps
希望这可以帮助
回答by Jia Xiao
Set bar item image's render mode to original can solve this problem. This can be done by using images in the .xcassets, so you don't have to write lots of codes.
将 bar item image 的渲染模式设置为 original 可以解决这个问题。这可以通过使用 .xcassets 中的图像来完成,因此您不必编写大量代码。
First step, drap&drop your bar item images to Assets.xcassets.
第一步,将您的栏项目图像拖放到 Assets.xcassets。
Second step, choose the bar item image, and change [Render As] to [Original Image]
第二步,选择bar item图片,将【Render As】改为【Original Image】
ps: I usually set the TabBarController's tab bar items all by story board to avoid writing lots of code.
ps:我通常通过故事板设置TabBarController的标签栏项目以避免编写大量代码。
回答by Amitabha
Add these lines of codes in
将这些代码行添加到
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.selectedImage = [[UIImage imageNamed:@"selectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"deselectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = @"xxxx";
tabBarItem2.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = @"xxxx";
tabBarItem3.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = @"xxxx";
tabBarItem4.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = @"xxxx";
return YES;
}
this works for me... and hope for the best...
这对我有用......并希望最好......
回答by Joakim
No answers helped fixing this issue. The main reason is that my TabBarControllerwasn't my RootViewController.
没有答案有助于解决此问题。主要原因是我的TabBarController不是我的RootViewController。
The solution I used for Storyboards, and I just clicked my UITabButtonand I added a runtime attribute for selectedImage:
我用于 Storyboards 的解决方案,我只是单击了我的,UITabButton然后添加了一个运行时属性selectedImage:


For each of the different views associated with the UITabController.
对于与UITabController.
回答by Pantelis Proios
After spending a couple of hours trying to make my custom tabbar to work for both iOS 6 & 7, that's what worked for me...
在花了几个小时试图让我的自定义标签栏同时适用于 iOS 6 和 7 之后,这对我有用......
UITabBarController *tabBarController = (UITabBarController *)[[self window] rootViewController];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.title = @"Home";
tabBarItem2.title = @"Map";
tabBarItem3.title = @"Weather";
tabBarItem4.title = @"Info";
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_home_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_home_black.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_cloud_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_cloud_black.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_map_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_map_black.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_info_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_info_black.png"]];
} else {
tabBarItem1.selectedImage = [[UIImage imageNamed:@"cyexplore_home_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"cyexplore_home_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.selectedImage = [[UIImage imageNamed:@"cyexplore_cloud_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"cyexplore_cloud_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.selectedImage = [[UIImage imageNamed:@"cyexplore_map_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"cyexplore_map_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.selectedImage = [[UIImage imageNamed:@"cyexplore_info_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"cyexplore_info_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
回答by 132206
if you are working with storyboards you have to put the identifier : "custom" in Navigation Controller.
如果您正在使用故事板,则必须在导航控制器中放置标识符:“自定义”。
then :
然后 :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
(void)[tabBarItem1 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
(void)[tabBarItem2 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
(void)[tabBarItem3 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
return YES;
}
This works for me.
这对我有用。
回答by jharr100
None of the answers worked for me - I am using MonoTouch but if you set the TintColorproperty of the UITabBaritself that will highlight the selected image with that color. In obj c it may be setTintColorfunction.
没有一个答案对我有用 - 我正在使用 MonoTouch 但如果您设置TintColor了UITabBar它本身的属性,它将用该颜色突出显示所选图像。在 obj c 中,它可能是setTintColor函数。
回答by Thành V?
You should write to the function:
您应该写入函数:
UIImage* tab_image = [UIImage imageNamed:@"tab_image.png"];
UIImage* tab_image_selected = [UIImage imageNamed:@"tab_image_selected.png"];
tab_image = [tab_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab_image_selected = [tab_image_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = tab_image;
self.tabBarItem.selectedImage = tab_image_selected;
I hope this helps
我希望这有帮助
回答by Haitham
I had the same problem with Xcode 6.0.1 (6A317), seems to be a bug with Interface builder.
However, i've managed to solve it by leaving selected imageempty in interface builder then at each viewDidLoadin my view controllers i did insert: [self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];
it works well now, showing my selectedImageand with global tint mask.
我在 Xcode 6.0.1 (6A317) 上遇到了同样的问题,似乎是 Interface builder 的一个错误。但是,我设法通过selected image在界面构建器中留空来解决它,然后viewDidLoad在我的视图控制器中的每个我插入:[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];
它现在运行良好,显示我的selectedImage和全局色调掩码。
回答by tymrtn
In your first view controller's .h file, I added the following: @property (weak, nonatomic) IBOutlet UITabBarItem *mapViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *profileViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *notificationViewTabBarItem;
在您的第一个视图控制器的 .h 文件中,我添加了以下内容:@property (weak, nonatomic) IBOutlet UITabBarItem *mapViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *profileViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *notificationViewTabBarItem;
(note that the mapViewTabBarItem was linked by ctrl dragging the actual tab bar item into the list of property declarations at the top of the .h file)
(注意 mapViewTabBarItem 是通过 ctrl 将实际的标签栏项拖到 .h 文件顶部的属性声明列表中来链接的)
Next, in the same view controller's .m file in the viewDidLoad, add the following:
接下来,在 viewDidLoad 中同一个视图控制器的 .m 文件中,添加以下内容:
self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
_mapViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-map-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
self.tabBarItem.image = [[UIImage imageNamed:@"@2x-map-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:1];
_profileViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-profile-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem.image = [[UIImage imageNamed:@"@2x-profile-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:2];
_notificationViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-notifications-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem.image = [[UIImage imageNamed:@"@2x-notifications-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

