49

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

20 回答 20

96

您需要使用 tabBarIteminitWithTitle: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];

希望这可以帮助

于 2013-09-19T17:26:01.093 回答
55

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

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

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

在此处输入图像描述

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

在此处输入图像描述

于 2015-10-21T08:45:16.767 回答
45

将这些代码行添加到

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

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

于 2013-09-23T06:35:12.287 回答
16

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

我用于情节提要的解决方案,我只是单击了我的UITabButton并添加了一个运行时属性selectedImage

对于与UITabController.

于 2015-05-13T13:02:08.893 回答
9

在花了几个小时试图让我的自定义标签栏同时适用于 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];
于 2013-11-21T00:26:38.800 回答
8

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

然后 :

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

这对我有用。

于 2013-10-23T23:38:10.980 回答
3

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

于 2013-10-15T16:19:20.513 回答
3

您应该写入函数:

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;

我希望这有帮助

于 2013-12-12T16:23:13.613 回答
3

我对 Xcode 6.0.1 (6A317) 有同样的问题,似乎是 Interface builder 的一个错误。但是,我设法通过selected image在界面构建器中留空然后在viewDidLoad我插入的每个视图控制器中解决它:

[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];

它现在运行良好,显示我的selectedImage和带有全局色调掩码。

于 2014-10-07T12:45:25.510 回答
2

根据 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]];
    }
}
于 2013-11-19T09:09:54.850 回答
2

在您的第一个视图控制器的 .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 ];
于 2014-06-23T07:05:26.603 回答
2

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

我的修复实际上很简单:在 IB 的属性检查器窗口下,“选定图像”的选项卡栏项目字段应该是空白的,“图像”的栏项目字段应该填充您想要的图像。

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

于 2014-10-01T20:07:22.293 回答
1

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

[[UITabBarItem alloc] initWithTitle:@"title" image:[[UIImage imageNamed:@"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
于 2013-12-17T13:00:24.157 回答
1

试试这个:

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];
于 2015-05-07T09:23:55.543 回答
0

我遇到过同样的问题。但是使用 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"]];
于 2013-11-20T10:19:53.240 回答
0

它是 UITabBar 项目的简单而干净的类别解决方案。

只需创建类别并使用运行时属性并从如下类别中引用它。 为选定的 TabBarItem 添加运行时属性 从类别中引用它并进行更改

#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
于 2015-07-10T04:02:57.470 回答
0

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

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

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

于 2015-12-30T01:39:04.650 回答
0

这是 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 保留为蓝色,则设置文本颜色部分很重要,因为它是默认设置。当您考虑标签栏项目的自定义图像时,这通常是实际的。

于 2016-04-15T15:39:21.200 回答
0

使用 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)
于 2016-08-16T11:28:03.723 回答
0

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

tabBarItem.image = UIImage(named: "image_name")
searchVC.tabBarItem.selectedImage = UIImage(named:
     "image_name_when_selected")?.imageWithRenderingMode(.AlwaysOriginal)
于 2016-10-12T06:03:17.003 回答