iOS 自定义TabBarItem,图片加文字,颜色自定义

[[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName:[UIColor colorWithRed:1.000green:0.314blue:0.000alpha:1]}forState:UIControlStateSelected];

HomeViewController*homeVC = [[HomeViewController alloc]initWithNibName:nil bundle:nil];

homeVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"商城"image:[UIImage imageNamed:@"menu_home_off"]selectedImage:[self originalImageName:@"menu_home_on"]];

UINavigationController*homeNavigation = [[UINavigationController alloc]initWithRootViewController:homeVC];

LocalViewController*localVC = [[LocalViewController alloc]initWithNibName:nil bundle:nil];

localVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"本地"image:[UIImage imageNamed:@"menu_merchant_off"]selectedImage:[self originalImageName:@"menu_merchant_on"]];

UINavigationController*localNavigation = [[UINavigationController alloc]initWithRootViewController:localVC];

ShoppingCarViewController*shoppingCarVC = [[ShoppingCarViewController alloc]initWithNibName:nil bundle:nil];

shoppingCarVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"购物车"image:[UIImage imageNamed:@"menu_cart_off"]selectedImage:[self originalImageName:@"menu_cart_on"]];

UINavigationController*shoppingCarNavigation = [[UINavigationController alloc]initWithRootViewController:shoppingCarVC];

MyViewController*myVC = [[MyViewController alloc]initWithNibName:nil bundle:nil];

myVC.tabBarItem= [[UITabBarItem alloc]initWithTitle:@"我的"image:[UIImage imageNamed:@"menu_account_off"]selectedImage:[self originalImageName:@"menu_account_on"]];

UINavigationController*myNavigation = [[UINavigationController alloc]initWithRootViewController:myVC];

UITabBarController*tabBarController = [[UITabBarController alloc]initWithNibName:nil bundle:nil];

tabBarController.viewControllers=@[homeNavigation,localNavigation,shoppingCarNavigation,myNavigation];

[UIApplication sharedApplication].delegate.window.rootViewController= tabBarController;


封装一个返回image的方法,用来设置tabBarItem的背景图片,使用原始图像

- (UIImage*)originalImageName:(NSString*)imageName

{

UIImage *img = [UIImage imageNamed:imageName];

return [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

}

设置tabbarItem下方文字被选中时的颜色


[[UITabBarItem appearance]setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName:[UIColor colorWithRed:1.000green:0.314blue:0.000alpha:1]}forState:UIControlStateSelected];

效果如图:


你可能感兴趣的:(iOS 自定义TabBarItem,图片加文字,颜色自定义)