在ios7中,方法setFinishedSelectedImage:withFinishedUnselectedImage: 已经被弃用,所以如果直接设置tabBarItem的selectImage和image时会出现不能显示图片的问题,效果可能如图:
这个跟自己先要的图片是明显不一样的,自己的图片如图:
另一个选中状态是白色的就不截图了,从上面已经可以看出来不一样了,下面来说下具体怎么写成自己想要的效果,效果如图:
怎么创建 就不说了,tabbarViewController大家都会创建,下面来说详细的设置
UITabBar *tabBar = self.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]; UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
UIImage *tabBarItem1Image = [UIImage imageNamed:@"11"]; UIImage *imgS1Image =[UIImage imageNamed:@"11s"]; tabBarItem1.selectedImage = [tabBarItem1Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem1.image = [imgS1Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *tabBarItem2Image = [UIImage imageNamed:@"12"]; UIImage *imgS2Image =[UIImage imageNamed:@"12s"]; tabBarItem2.selectedImage = [tabBarItem2Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem2.image = [imgS2Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *tabBarItem3Image = [UIImage imageNamed:@"13"]; UIImage *imgS3Image =[UIImage imageNamed:@"13s"]; tabBarItem3.selectedImage = [tabBarItem3Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem3.image = [imgS3Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *tabBarItem4Image = [UIImage imageNamed:@"14"]; UIImage *imgS4Image =[UIImage imageNamed:@"14s"]; tabBarItem4.selectedImage = [tabBarItem4Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem4.image = [imgS4Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *tabBarItem5Image = [UIImage imageNamed:@"15"]; UIImage *imgS5Image =[UIImage imageNamed:@"15s"]; tabBarItem5.selectedImage = [tabBarItem5Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBarItem5.image = [imgS5Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImageRenderingModeAutomatic// 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
UIImageRenderingModeAlwaysOriginal// 始终绘制图片原始状态,不使用Tint Color。
UIImageRenderingModeAlwaysTemplate,// 始终根据Tint Color绘制图片,忽略图片的颜色信息。
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f], NSForegroundColorAttributeName : [UIColor orangeColor] } forState:UIControlStateNormal];//未选中 [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f], NSForegroundColorAttributeName : [UIColor greenColor] } forState:UIControlStateSelected];//选中
//设置选中item后,显示在此item下面的图? tabBar.selectionIndicatorImage = [UIImage imageNamed:@"select_bg"]; //设置tabBar的背景图? self.tabBar.backgroundImage = [UIImage imageNamed:@"show_bg"];
原文地址:http://blog.csdn.net/xiaopeng12300/article/details/44244085