首先,项目是在故事板中搭建的,所以遇到这个问题的时候,首先是想到在故事板中找到相关的属性,确实是有一个Selected Image,但是设置了这个图片以后,运行的效果是,点击选择后,本身的image就没有了;
(不知道这个属性是做什么用的)
然后就想着还是用代码添加吧,于是就有了下面的一大段代码
TableViewController *tv = [[TableViewController alloc]init];
TableViewController2 *tv2 = [[TableViewController2 alloc]init];
TableViewController3 *tv3 = [[TableViewController3 alloc]init];
TableViewController4 *tv4 = [[TableViewController4 alloc]init];
[self setViewControllers:@[tv,tv2,tv3,tv4]];
// 拿到 TabBar 在拿到想应的item
UITabBar *tabBar = self.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:@"食课B"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item0.image = [[UIImage imageNamed:@"食课A"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.selectedImage = [[UIImage imageNamed:@"食谱B"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item1.image = [[UIImage imageNamed:@"食谱A"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.selectedImage = [[UIImage imageNamed:@"食课B"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item2.image = [[UIImage imageNamed:@"食课A"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item3.selectedImage = [[UIImage imageNamed:@"食谱B"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
item3.image = [[UIImage imageNamed:@"食谱A"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
效果确实是出来了,但是很明显,就为了一个点击后修改颜色的效果,这段代码实在太多
然后接着网上找,发现了
self.tabBar.tintColor = [UIColor colorWithRed:255.0/255.0 green:124.0/255.0 blue:56.0/255.0 alpha:1];
众里寻她千百度,,,
这些代码都不是在Appdelegate中写的,是在一个继承自UITabBarController的控制器(也是项目的根控制器,不然能self.吗?)中写的。所以,如果你只是想单纯的修改tabbar选择后的颜色的话,那么上面的一大段代码可以省略(不过用代码创建tabbar的时候可以借鉴),