1.有时候设置tabitem图片时,图片未选中时正常,选中后变的超大,这时就是需要我们给图片资源设置好原图和2x即可。
2.当我们用这串代码给tabbaritem设置图片时,会有问题发生,当选中后,图片保留的不是本身的颜色,而是默认的蓝色。
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ViewController *viewController = [[ViewController alloc]init];
viewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:xx selectedImage:xx];
tabBarController.viewControllers = @[[[UINavigationController alloc]initWithRootViewController:viewController],[[UINavigationController alloc]initWithRootViewController:homePageViewController2],[[UINavigationController alloc]initWithRootViewController:homePageViewController3]];
解决方法:
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ViewController *homePageViewController = [[ViewController alloc]init];
ViewController *homePageViewController2 = [[ViewController alloc]init];
ViewController *homePageViewController3 = [[ViewController alloc]init];
ViewController *homePageViewController4 = [[ViewController alloc]init];
ViewController *homePageViewController5 = [[ViewController alloc]init];
tabBarController.viewControllers = @[[[UINavigationController alloc]initWithRootViewController:homePageViewController],[[UINavigationController alloc]initWithRootViewController:homePageViewController2],[[UINavigationController alloc]initWithRootViewController:homePageViewController3],[[UINavigationController alloc]initWithRootViewController:homePageViewController4],[[UINavigationController alloc]initWithRootViewController:homePageViewController5]];
// 拿到 TabBar 在拿到想应的item
UITabBar *tabBar = tabBarController.tabBar;
//设置被选中时显示是原图
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
item0.image = [[UIImage imageNamed:@"首页"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item0.selectedImage = [[UIImage imageNamed:@"首页选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item0.title = @"首页";
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
item1.image = [[UIImage imageNamed:@"报表"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.selectedImage = [[UIImage imageNamed:@"报表选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.title = @"店铺报表";
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
item2.image = [[UIImage imageNamed:@"论坛"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.selectedImage = [[UIImage imageNamed:@"论坛选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.title = @"论坛";
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
item3.image = [[UIImage imageNamed:@"购物车"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item3.selectedImage = [[UIImage imageNamed:@"购物车选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item3.title = @"购物车";
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
item4.image = [[UIImage imageNamed:@"个人中心"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.selectedImage = [[UIImage imageNamed:@"个人中心选"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.title = @"个人中心";
UIColor *titleNormalColor = [UIColor colorWithRed:146 green:146 blue:146 alpha:1];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleNormalColor, UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:226 green:6 blue:37 alpha:1];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil] forState:UIControlStateSelected];
// 拿到 TabBar 在拿到想应的item
UITabBar *tabBar = tabBarController.tabBar;
//修改字体颜色
tabBar.tintColor = [UIColor redColor];