以后一定要注意苹果官方文档
iOS 8
@implementationUITabBarItem (Universal)
+ (instancetype)itemWithTitle:(NSString*)title image:(UIImage*)image selectedImage:(UIImage*)selectedImage
{
UITabBarItem*tabBarItem =nil;
if([[UIDevicecurrentDevice]systemVersion].floatValue>=7) {
image = [imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //这两个地方一定要加上
selectedImage = [selectedImageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem = [[UITabBarItemalloc]initWithTitle:titleimage:imageselectedImage:selectedImage];
}else{
tabBarItem = [[UITabBarItemalloc]init];
[tabBarItemsetFinishedSelectedImage:selectedImagewithFinishedUnselectedImage:image];
}
returntabBarItem;
}
@end
.....
[[self.tabBarControllertabBar]setBackgroundImage:[UIImageimageNamed:@"dhbj.png"]];
[[self.tabBarControllertabBar]setSelectionIndicatorImage:[UIImageimageNamed:@"dhbj_1.png"]];
使用举例 :
UIViewController*chargeController=[[[ChargingViewControlleralloc]init]autorelease];
chargeController.tabBarItem= [UITabBarItemitemWithTitle:nilimage:[UIImageimageNamed:@"daohang_2.png"]selectedImage:[UIImageimageNamed:@"daohang_2_liang.png"]];
UINavigationController*chargeNav=[[[UINavigationControlleralloc]initWithRootViewController:chargeController]autorelease];
以前的这种写法废弃了:
for(UITabBarItem *tabIteminself.tabBarController.tabBar.items)
{
if(tabItem==[self.tabBarController.tabBar.items objectAtIndex:0])
{
[tabItem setFinishedSelectedImage:[UIImage imageNamed:@"daohang_1_liang.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"daohang_1"]];
}
elseif(tabItem==[self.tabBarController.tabBar.items objectAtIndex:1])
{
[tabItem setFinishedSelectedImage:[UIImage imageNamed:@"daohang_2_liang.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"daohang_2"]];
}
elseif(tabItem==[self.tabBarController.tabBar.items objectAtIndex:2])
{
[tabItem setFinishedSelectedImage:[UIImage imageNamed:@"daohang_3_liang.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"daohang_3"]];
}
elseif(tabItem==[self.tabBarController.tabBar.items objectAtIndex:3])
{
[tabItem setFinishedSelectedImage:[UIImage imageNamed:@"daohang_4_liang.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"daohang_4"]];
}
}
网友评论