让TabBarItem 显示出我们设置的图片的真实样子,需要看以下设置方法在AppDelegate.h中,拿到所有的item然后统一设置成自定义的图片分别是选中和未选中,针对ios7以后
_tabBarController = [[UITabBarController alloc]init];
[_tabBarController setViewControllers:@[recNav,feedNav,postNav,loginNav]];
// 拿到 TabBar 在拿到想应的item
UITabBar *tabBar = _tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
// 对item设置相应地图片
item0.selectedImage = [[UIImage imageNamed:@"recognize-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item0.image = [[UIImage imageNamed:@"recognize"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.selectedImage = [[UIImage imageNamed:@"life-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item1.image = [[UIImage imageNamed:@"life"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.selectedImage = [[UIImage imageNamed:@"edit-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item2.image = [[UIImage imageNamed:@"edit"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item3.selectedImage = [[UIImage imageNamed:@"setting-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item3.image = [[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
通过以上代码设置就把4个item的图片设置好了当然也可以分别在对应的controller中进行相应地设置,不过建议统一设置修改起来比较省事,如果需要在item点击有很炫的动画或是其他样式,那就需要高度自定义才能实现
另一种思路
IOS8以前使用的是:
UITabBarItem *photoItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"app2"] tag:0];
photo.tabBarItem = photoItem;
[photo.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"app1"] withFinishedUnselectedImage:[UIImage imageNamed:@"app2"]];
[photoItem release];
使用新的API方法来初始化tabBarItem
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage
同时对使用的UIImage设置一下UIImageRenderingMode,使用UIImageRenderingModeAlwaysOriginal
用例:
UIImage * normalImage = [[UIImage imageNamed:@"nor.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage * selectImage = [[UIImage imageNamed:@"select.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem * tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:normalImage selectedImage:selectImage];