在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后,显示在此item下面的图? tabBar.selectionIndicatorImage = [UIImage imageNamed:@"select_bg"]; //设置tabBar的背景图? self.tabBar.backgroundImage = [UIImage imageNamed:@"show_bg"];
大功告成!
时间: 2024-10-10 05:26:18