UITabBar不显示在iOS 7中选定的项目图像
这些图标在ios 6中显示正常,但在ios 7中显示不了。我在viewController的viewDidLoad方法中设置了选中的状态。 当用户select一个标签栏项目图像消失。 这是我的代码:
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
[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];
结合更改UIImage渲染模式:
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
或(要应用父视图模板色调蒙版,此选项是标签栏项目的默认选项,除非您select使用上述渲染模式)
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];
希望这可以帮助
添加这些代码行
- (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; }
这对我有用…并希望最好的…
将条形图图像的渲染模式设置为原来可以解决这个问题。 这可以通过使用.xcassets中的图像来完成,因此您不必编写大量的代码。
第一步,将您的酒吧项目图像拖放到Assets.xcassets。
第二步,select条形图项目,将[渲染为]更改为[原始图像]
PS:我通常通过故事板设置TabBarController的标签栏项目,以避免写很多代码。
没有答案帮助解决了这个问题。 主要原因是我的TabBarController
不是我的RootViewController
。
我用于Storyboard的解决scheme,我只是点击了我的UITabButton
并为selectedImage
添加了一个运行时属性:
对于与UITabController
关联的每个不同的视图。
花了几个小时试图让我的自定义tabbar适用于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];
如果您正在使用故事板,则必须在“导航控制器”中放置标识符“custom”。
然后 :
- (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; }
这对我有用。
没有答案为我工作 – 我使用的是MonoTouch,但如果您设置UITabBar
本身的TintColor
属性,将使用该颜色突出显示选定的图像。 在obj c它可能是setTintColor
函数。
我有与Xcode 6.0.1(6A317)相同的问题,似乎是与接口生成器的错误。 但是,我设法解决它通过在界面生成器中保留selected image
空,然后在我的视图控制器中的每个viewDidLoad
插入:
[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];
它现在运行良好,显示我selectedImage
和全球色调面具。
根据132206的回答,我为AppDelegate创build了这个方法,从application:didFinishLaunchingWithOptions:
调用application:didFinishLaunchingWithOptions:
with:
[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]]; } }
你应该写这个函数:
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;
我希望这有帮助
在您的第一个视图控制器的.h文件中,我添加了以下内容:@property(弱,非primefaces)IBOutlet UITabBarItem * mapViewTabBarItem; @属性(弱,非primefaces)IBOutlet UITabBarItem * profileViewTabBarItem; @属性(弱,非primefaces)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 ];
我有一个类似的问题。 我在故事板中创build了标签栏,并通过界面生成器菜单添加了所有图像,而代码中则没有。
我的修复其实很简单:在IB的属性检查器窗口下,“选定的图像”的选项栏项目字段应该是空白的,而“图像”的条形图项目字段应该填充你想要的图像。
我正在运行Xcode 6.0.1并使用iOS 8.0.2设备进行testing。
尝试这个:
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];
我遇到过同样的问题。 但是使用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"]];
使用下面的代码来修复iOS7中的图像问题:
[[UITabBarItem alloc] initWithTitle:@"title" image:[[UIImage imageNamed:@"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
这是UITabBar项目的类别的简单和干净的解决scheme。
只需创build类别并使用运行时属性并从下面的类别中引用它。
#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
旧的问题,但是在Xamarin.iOS / C#和那些想要通过接口生成器设置图像的地方也要添加我的解决scheme。 我通过Interface Builder设置了Selected Image
和Image
属性。 然后在代码中,我定义了一个像这样的InitTabs()
方法:
public void InitTabs(){ HomeTab.SelectedImage = HomeTab.SelectedImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); HomeTab.Image = HomeTab.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); }
在ViewDidLoad
调用InitTabs()
,现在适当的图像将出现在选中状态和非选中状态。
这是Swift-Guys的Swift解决scheme:)
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循环选项卡栏项目,并刚创buildbtn标题数组
- 从数组中设置barButtonItem的图像和它的selectedImage
- 从数组中设置标题文本
- 为状态.Normal和.Selected设置标题文本颜色
设置文本颜色的一部分是非常重要的,如果你不想保持该项目的标题颜色灰色。正常和蓝色的。选定,因为它是默认情况下。 当您考虑标签栏项目的自定义图像时,这通常是实际的。
使用UIAppearance API显示选定和未select的图像和标题的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)
在尝试了所有其他的答案之后,我们find了答案。 其他答案似乎并不适用于目前的迅速版本。 在Swift 2.3中,这对我很有用。 对于那些仍然有麻烦的人,试试这个:
tabBarItem.image = UIImage(named: "image_name") searchVC.tabBarItem.selectedImage = UIImage(named: "image_name_when_selected")?.imageWithRenderingMode(.AlwaysOriginal)