UITabBar 未在 ios 7 中显示所选项目图像

Posted

技术标签:

【中文标题】UITabBar 未在 ios 7 中显示所选项目图像【英文标题】:UITabBar not showing selected item images in ios 7 【发布时间】:2013-09-19 12:46:12 【问题描述】:

图标在 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];

【问题讨论】:

【参考方案1】:

你需要使用tabBarItem initWithTitle:image:selectedImage

[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];

结合改变UIImage渲染模式:

imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

或(应用父视图模板色调掩码,此选项是选项卡栏项的默认选项,除非您选择退出上述渲染模式)

imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate

这是一个标签栏项目的代码示例:-

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];

希望对你有帮助

【讨论】:

@Rushabh 我需要更多信息,你在哪里从 Appdelegate、viewController 或 UITabbar/Controller 子类或类别中调用它 这仅适用于其中一张图片。使用这个确切的代码未选择的选项卡项目看起来像我设置的图标。被选中的只是一个蓝色圆圈。 我目前遇到了同样的问题,但仅在运行安装了 iOS 7 的非视网膜设备时。 在 iOS 7.1 中,不需要此解决方法。我想知道这是否是一个错误? 请注意In conjunction with changing the UIImage rendering mode: imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal 是一个非常重要的细节。如果您的图像配置为模板,则会出现默认的系统颜色。因此,不要尝试item.selectedImage = someImage;,而是尝试item.selectedImage = [someImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];,看看它是否有效。【参考方案2】:

设置bar item图片的渲染模式为原始可以解决这个问题。 这可以通过使用 .xcassets 中的图像来完成,因此您不必编写大量代码。

第一步,将您的条形项目图像拖放到 Assets.xcassets。

第二步,选择bar item图片,将[Render As]改为[Original Image]

ps:我一般都是通过storyboard来设置TabBarController的tab bar项,避免写很多代码。

【讨论】:

更简洁的方法来修复标签栏图像问题。谢谢你 谢谢贾晓! 哇!非常感谢 这个答案必须被接受,因为不需要编写代码来显示来自 StoryBoard 的原始图像。伟人坚持下去【参考方案3】:

中加入这几行代码
- (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;

这对我有用...希望最好...

【讨论】:

这适用于 iOS7,仅供参考,早期的 SDK 上没有方法 [UIImage imageWithRenderingMode]。 在 iOS7 上不适合我。背景图像和标签栏项目的图像均未显示。 谢谢!这件事让我头疼了 2 个小时。文档说要使用方法 initWithTitle:image:selectedImage:...但这对我根本不起作用!你的方法很有效 也适用于 iOS 8+。【参考方案4】:

没有答案有助于解决此问题。主要原因是我的TabBarController不是我的RootViewController

我用于 Storyboards 的解决方案,我只是单击了我的 UITabButton 并为 selectedImage 添加了运行时属性:

对于与UITabController 关联的每个不同视图。

【讨论】:

非常感谢您的回答!【参考方案5】:

在花了几个小时试图让我的自定义标签栏同时适用于 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];

【讨论】:

【参考方案6】:

如果您正在使用情节提要,则必须将标识符:“自定义”放在导航控制器中。

然后:

- (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;

这对我有用。

【讨论】:

非常感谢。我在 NavigationController 中没有“自定义”。【参考方案7】:

没有一个答案对我有用 - 我正在使用 MonoTouch,但如果您设置 UITabBar 本身的 TintColor 属性,它将用该颜色突出显示所选图像。在 obj c 中可能是 setTintColor 函数。

【讨论】:

谢谢你,jharr100,这解决了我的问题。 :) 我希望我能投票两次!【参考方案8】:

你应该写入函数:

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;

希望对你有帮助

【讨论】:

如果您不知道,您可以使用markdown 在您的帖子正文中格式化代码 sn-ps。这有助于使您的问题/答案更加简洁易读。此外,请确保您确实在回答原始发帖人的问题。【参考方案9】:

我在使用 Xcode 6.0.1 (6A317) 时遇到了同样的问题,这似乎是 Interface builder 的错误。 但是,我设法通过在界面生成器中将 selected image 留空然后在我的视图控制器中插入的每个 viewDidLoad 来解决它:[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]]; 它现在运行良好,显示我的selectedImage 并带有全局色调遮罩。

【讨论】:

【参考方案10】:

根据 132206 的回答,我为 AppDelegate 制作了这个方法,从application:didFinishLaunchingWithOptions: 调用:

[self configureSelectedImages];

它显然需要对标签图像进行严格的命名约定,但也可以在不进行编辑的情况下重复使用。显而易见 - 将您选择的标签栏图像命名为 TabbarXXXSelected,其中 XXX 等于标签栏项目的标题。

- (void)configureSelectedImages

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;

    for (UITabBarItem *tabBarItem in [tabBar items]) 
        NSString *selectedImage = [NSString stringWithFormat:@"Tabbar%@Selected", [tabBarItem title]];
        (void)[tabBarItem initWithTitle:[tabBarItem title] image:[tabBarItem image] selectedImage:[UIImage imageNamed:selectedImage]];
    

【讨论】:

【参考方案11】:

在您的第一个视图控制器的 .h 文件中,我添加了以下内容: @property (weak, nonatomic) IBOutlet UITabBarItem *mapViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *profileViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *notificationViewTabBarItem;

(请注意,mapViewTabBarItem 是通过 ctrl 将实际的标签栏项目链接到 .h 文件顶部的属性声明列表中)

接下来,在 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 ];

【讨论】:

【参考方案12】:

我遇到了类似的问题。我在情节提要中创建了标签栏,并通过界面构建​​器菜单添加了所有图像,代码中没有。

我的解决方法其实很简单:在 IB 的属性检查器窗口下,“Selected Image”的 Tab Bar Item 字段应该是空白的,“Image”的 Bar Item 字段应该填入你想要的图像。

我正在运行 Xcode 6.0.1 并使用 iOS 8.0.2 设备进行测试。

【讨论】:

【参考方案13】:

试试这个:

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-Small-50.png"] tag:100];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"image-50.png"] tag:200];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-clip-50.png"] tag:300];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-color-50.png"] tag:400];
UITabBarItem *item5 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-lock-50.png"] tag:500];

[item1 setSelectedImage:[[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item2 setSelectedImage:[[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item3 setSelectedImage:[[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item4 setSelectedImage:[[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item5 setSelectedImage:[[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item5 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

item1.image = [[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item2.image = [[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item3.image = [[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.image = [[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item5.image = [[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

【讨论】:

能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?【参考方案14】:

使用以下代码修复iOS7中的图像问题:

[[UITabBarItem alloc] initWithTitle:@"title" image:[[UIImage imageNamed:@"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

【讨论】:

【参考方案15】:

我有同样的问题。但是使用 StoryBoards 使我无法更改代码中的任何内容。在情节提要中将图像留空为我消除了这个障碍。然而,将 initWithTitle 放在选项卡视图控制器的 viewWillAppear 方法中给了我奇怪的行为。首先获取选定的图像需要额外单击,并且图像不会显示在非初始选项卡中。

对我来说,解决这个问题是将以下代码添加到 DidFinishLoadingWithOptions 中的 AppDelegate(类似于 132206 和 Amitabha):

NSArray * vcs = [(UITabBarController*)self.window.rootViewController viewControllers];
UIViewController *tab0 = [[(UINavigationController*)[vcs objectAtIndex:0] viewControllers] objectAtIndex:0];
tab0.title = NSLocalizedString(@"Time", nil);
tab0.tabBarItem =  [[UITabBarItem alloc] initWithTitle:tab0.title image:[UIImage imageNamed:@"Recents.png"] selectedImage:[UIImage imageNamed:@"RecentsSolid.png"]];
UIViewController *tab1 = [[(UINavigationController*)[vcs objectAtIndex:1] viewControllers] objectAtIndex:0];
tab1.title = NSLocalizedString(@"Expense", nil);
tab1.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab1.title image:[UIImage imageNamed:@"Euro.png"] selectedImage:[UIImage imageNamed:@"EuroSolid.png"]];

【讨论】:

【参考方案16】:

UITabBar Items 类别的简单而干净的解决方案。

只需创建类别并使用运行时属性并从如下类别中引用它。

#import "UITabBarItem+CustomTabBar.h"

@implementation UITabBarItem (CustomTabBar)

-(void)setValue:(id)value forKey:(NSString *)key 
    if([key isEqualToString:@"tabtitle"])
        if([value isEqualToString:@"contacts"]) 
            [self setSelectedImage:[[UIImage imageNamed:@"contacts-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
         else if([value isEqualToString:@"chat"]) 
            [self setSelectedImage:[[UIImage imageNamed:@"chat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
         else if([value isEqualToString:@"groupchat"]) 
            [self setSelectedImage:[[UIImage imageNamed:@"groupchat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
         else if([value isEqualToString:@"settings"]) 
            [self setSelectedImage:[[UIImage imageNamed:@"settings-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        
    
    [self setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Roboto-Regular" size:12.0f], NSFontAttributeName, [UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];


@end

【讨论】:

【参考方案17】:

老问题,但也将为 Xamarin.iOS/C# 和那些想要通过界面生成器设置图像的人添加我的解决方案。我通过 Interface Builder 设置了 Selected ImageImage 属性。然后在代码中我定义了一个InitTabs() 方法,如下所示:

public void InitTabs()
        HomeTab.SelectedImage = HomeTab.SelectedImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
        HomeTab.Image = HomeTab.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

ViewDidLoad 中调用InitTabs(),现在将为选中和未选中状态显示正确的图像。

【讨论】:

【参考方案18】:

这是 Swift-Guys 的 Swift 解决方案 :)

class CustomTabBar: UITabBar 

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)

        let btnNames = ["Title1", "Title2", "Title3", "Title4"]

        for (item, name) in zip(items!, btnNames) 
            item.image = UIImage(named: "bar\(name)Btn")?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "bar\(name)SelectedBtn")?.imageWithRenderingMode(.AlwaysOriginal)
            item.title = name
            item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: .Normal)
            item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Selected)
        
    


这里到底发生了什么:

制作 btn 标题数组并考虑图像文件名以匹配它们 在标签栏项目上进行 For 循环并刚刚创建 btn 标题数组 从数组中设置 barButtonItem 的图像及其 selectedImage 从数组中设置标题文本 为状态 .Normal 和 .Selected 设置标题文本颜色

如果您不想为 .Normal 保留项目的标题颜色,而为 .Selected 保留项目的标题颜色,则设置文本颜色部分很重要,因为默认情况下是这样。当您考虑标签栏项目的自定义图像时,这通常是实际的。

【讨论】:

【参考方案19】:

使用 UIAppearance API 显示选定和未选定的图像和标题的 Swift 版本 在您的 Appdelegate.m 中复制以下代码,如果您有标签库 app.following 代码假设您有 4 个标签栏。

let tabBarController: UITabBarController = (self.window!.rootViewController as! UITabBarController)

    let tabBar:UITabBar = tabBarController.tabBar

    let tabBarItem1:UITabBarItem = tabBar.items![0]
    let tabBarItem2:UITabBarItem = tabBar.items![1]
    let tabBarItem3:UITabBarItem = tabBar.items![2]
    let tabBarItem4:UITabBarItem = tabBar.items![3]

    tabBarItem1.title = "Home";
    tabBarItem2.title = "Maps";
    tabBarItem3.title = "My Plan";
    tabBarItem4.title = "Settings";

    tabBarItem1.selectedImage = UIImage(named: "home_selected.png")!
    tabBarItem2.selectedImage = UIImage(named: "maps_selected.png")!
    tabBarItem3.selectedImage = UIImage(named: "myplan_selected.png")!
    tabBarItem4.selectedImage = UIImage(named: "settings_selected.png")!

     tabBarItem1.image =  UIImage(named: "home.png")!
     tabBarItem2.image =  UIImage(named: "maps.png")!
     tabBarItem3.image =  UIImage(named: "myplan.png")!
     tabBarItem4.image =  UIImage(named: "settings.png")!


    let tabBarBackground: UIImage = UIImage(named: "tabbar.png")!
    UITabBar.appearance().backgroundImage = tabBarBackground
    UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")!


    UITabBarItem.appearance().setTitleTextAttributes([
        NSForegroundColorAttributeName : UIColor.whiteColor()
        ]
        , forState: .Normal)
    let titleHighlightedColor: UIColor = UIColor(red: 153 / 255.0, green: 192 / 255.0, blue: 48 / 255.0, alpha: 1.0)
    UITabBarItem.appearance().setTitleTextAttributes([
        NSForegroundColorAttributeName : titleHighlightedColor
        ]
        , forState: .Highlighted)

【讨论】:

【参考方案20】:

在尝试了所有其他答案并因失败而苦苦挣扎后,我找到了答案。其他答案,似乎在当前的 swift 版本中不起作用。在 Swift 2.3 中,这对我有用。对于那些仍然有问题的人,试试这个:

tabBarItem.image = UIImage(named: "image_name")
searchVC.tabBarItem.selectedImage = UIImage(named:
     "image_name_when_selected")?.imageWithRenderingMode(.AlwaysOriginal)

【讨论】:

以上是关于UITabBar 未在 ios 7 中显示所选项目图像的主要内容,如果未能解决你的问题,请参考以下文章

iOS 7.1 打破 UITabbar 图像选定状态

UITabBar 所选项目色调颜色

IOS:UITabbar 项目单击一次又一次,它正在减少IOS 7 中的UITabbar 按钮项目大小

iOS 7 uitabbar 显示,在 ios8 上不可见

UITabbar 项目不显示,直到单击升级到 Xcode 7

UIAlertView 按钮(子视图)未在 iOS 7 中显示