iOS7 故事版创建tanbar
在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];
创建5个UITabBarItem,然后就開始设置Item的图片了
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];
上面的设置的方法就是iOS7以后用的。着色(Tint Color)是iOS7界面中的一个重大改变,你能够设置一个UIImage在渲染时是否使用当前视图的Tint Color。UIImage新增了一个仅仅读属性:renderingMode。相应的另一个新增方法:imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属性。
该枚举中包括下列值:
UIImageRenderingModeAutomatic// 依据图片的使用环境和所处的画图上下文自己主动调整渲染模式。
UIImageRenderingModeAlwaysOriginal// 始终绘制图片原始状态。不使用Tint Color。
UIImageRenderingModeAlwaysTemplate,// 始终依据Tint Color绘制图片,忽略图片的颜色信息。
接下来改动item的文字。代码例如以下
[[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"];
大功告成!