自定义UITabBar图标iOS

我正在开发一个选项卡式应用程序,我想集成自定义的UITabbar背景和图标。 我已经插入了一个自定义的背景,但我有一个图标的问题。

这是图标应该看起来如何:

在这里输入图像说明

但添加故事板中的每个视图的图标后,我得到这样的灰色图标:

在这里输入图像说明

有没有人有一个想法,为什么这样的图标显示?

非常感谢你!

这是我在我的一个应用程序中做的。 在你的AppDelegates didFinishLaunchingWithOptions:方法中,添加这个并填写你的图像名称:

  UITabBarController *tabController = (UITabBarController *)self.window.rootViewController; //tab1 UIImage *selectedImage = [UIImage imageNamed:@"home-tab-selected"]; UIImage *unselectedImage = [UIImage imageNamed:@"home2-tab"]; MyTabBar *tabBar = (MyTabBar *)tabController.tabBar; UITabBarItem *item1 = [tabBar.items objectAtIndex:0]; [item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage]; //tab2 selectedImage = [UIImage imageNamed:@"customers-tab-selected"]; unselectedImage = [UIImage imageNamed:@"customers-tab"]; UITabBarItem *item2 = [tabBar.items objectAtIndex:1]; [item2 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage]; //tab3 selectedImage = [UIImage imageNamed:@"maps-tab-selected"]; unselectedImage = [UIImage imageNamed:@"maps-tab"]; UITabBarItem *item3 = [tabBar.items objectAtIndex:2]; [item3 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage]; //tab4 selectedImage = [UIImage imageNamed:@"reports-tab-selected"]; unselectedImage = [UIImage imageNamed:@"reports-tab"]; UITabBarItem *item4 = [tabBar.items objectAtIndex:3]; [item4 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage]; //tab5 selectedImage = [UIImage imageNamed:@"orders-tab-selected"]; unselectedImage = [UIImage imageNamed:@"orders-tab"]; UITabBarItem *item5 = [tabBar.items objectAtIndex:4]; [item5 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage]; if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) { // ios 5 code here [tabBar setBackgroundImage:[UIImage imageNamed:@"tab-bg"]]; } 

为我完美的作品。

使用Xcode 6,为了能够直接从Storyboard中改变图像,你可以这样做:

 @IBDesignable class CustomizedTabBarItem: UITabBarItem { @IBInspectable var finalImage:UIImage = UIImage() { didSet { self.image = finalImage.imageWithRenderingMode(.AlwaysOriginal) } } @IBInspectable var finalSelectedImage:UIImage = UIImage() { didSet { self.selectedImage = finalSelectedImage.imageWithRenderingMode(.AlwaysOriginal) } } } 

之后,您只需在故事板中设置图像 在这里输入图像说明

我现在还不能评论,所以我会加上回答。

由于iOS 7 setFinishedSelectedImage:withFinishedUnselectedImage:已被弃用。

要解决这个问题,你可以这样做:

 UIImage *selectedImage = [UIImage imageNamed:@"tabbar-highlight"]; selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *unselectedImage = [UIImage imageNamed:@"tabbar"]; unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem = [[UITabBarItem alloc] initWithTitle:@"TabBarItem1" image:unselectedImage selectedImage:selectedImage]; // //OR // UIImage *selectedImage = [UIImage imageNamed:@"tabbar-highlight"]; selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *unselectedImage = [UIImage imageNamed:@"tabbar"]; unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [yourTabBarItem setImage:unselectedImage]; [yourTabBarItem setSelectedImage:selectedImage]; 

希望这个帮助澄清iOS7 +。

您可以使用setFinishedSelectedImage:withFinishedUnselectedImage:设置标签栏图标的完成图像setFinishedSelectedImage:withFinishedUnselectedImage: on UITabBarItem

在这里查看文档(看下面的外观)

UITabBarItem的默认行为是根据你的图标创build一个遮罩,然后在你的UITabBarItem中应用特殊的蓝色处理。

你也可以保持简单;)

  [tabBarItemName setSelectedImage:[UIImage imageNamed:@"image name"]]; 

你必须在你的ViewController.h中声明tabBarItemName