在项目中,发现tabbar设置的图片老是出现模糊的情况,查了一些资料还是没有解决,后来发现是没有适配Retina。
要想适配Retina不要美工处理3种格式的图片 30*30,@2x,@3x 三个尺寸的图片才行!
#import <UIKit/UIKit.h>
@interface TabBarViewController : UITabBarController
@end
#import "TabBarViewController.h"
#import "aaaViewController.h"
@implementation TabBarViewController
-(void)initControlles{
aaaViewController *aa = [[aaaViewController alloc] init];
UINavigationController* ayNav = [[UINavigationController alloc]initWithRootViewController:aa];
aaaViewController *bb = [[aaaViewController alloc] init];
UINavigationController* bNav = [[UINavigationController alloc]initWithRootViewController:bb];
aaaViewController *cc = [[aaaViewController alloc] init];
UINavigationController* cNav = [[UINavigationController alloc]initWithRootViewController:cc];
self.viewControllers=[[NSArray alloc]initWithObjects:ayNav,bNav,cNav, nil];
}
-(void)initItemsImage{
UIImage *image1=[UIImage imageNamed:@"2_1"];
UIImage *selectImage1=[UIImage imageNamed:@"2_1"];
NSArray *imagesArray=@[image1,image1,image1];
NSArray *selectImagesArray=@[selectImage1,selectImage1,selectImage1];
NSArray *tites=@[@"aaa",@"bbb",@"ccc"];
for (int i=0;i<self.tabBar.items.count;i++) {
UITabBarItem *item=self.tabBar.items[i];
item.title=tites[i];
UIImage *image=imagesArray[i];
UIImage *selectImage=selectImagesArray[i];
item.image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.selectedImage=[selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}
-(void)initAttributes{
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
NSForegroundColorAttributeName,
nil];
NSDictionary *selectDic=[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],
NSForegroundColorAttributeName,
nil];
[[UITabBarItem appearance] setTitleTextAttributes:dic forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:selectDic forState:UIControlStateSelected];
UIImage* tabBarBackground = [UIImage imageNamed:@"navi_44.png"];
[[UITabBar appearance]setBackgroundImage:tabBarBackground];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initControlles];
[self initItemsImage];
[self initAttributes];
self.selectedIndex=0;
}