如何更改标签栏上不活动的图标/文字颜色?

如何在iOS 7标签栏上更改不活动的图标/文字颜色? 灰色的那个。

在这里输入图像说明

在每个TabBar的每个第一个ViewController中:

- (void)viewDidLoad { [super viewDidLoad]; // changing the unselected image color, you should change the selected image // color if you want them to be different self.tabBarItem.selectedImage = [[UIImage imageNamed:@"yourImage_selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.tabBarItem.image = [[UIImage imageNamed:@"yourImage_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } 

这个代码的线索是'UIImageRenderingModeAlwaysOriginal':

Apple文档的渲染模式:

 UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information 

要更改文字颜色:

在AppDelegate中:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Add this if you only want to change Selected Image color // and/or selected image text [[UITabBar appearance] setTintColor:[UIColor redColor]]; // Add this code to change StateNormal text Color, [UITabBarItem.appearance setTitleTextAttributes: @{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal]; // then if StateSelected should be different, you should add this code [UITabBarItem.appearance setTitleTextAttributes: @{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected]; return YES; } 

您也可以直接将资产目录中的标签栏图像设置为“ Render As ”属性。 您可以select将该属性设置为DefaultOriginal ImageTemplate Image

设置图像的渲染选项

用于改变tabbar的非select图标的颜色

对于iOS 10以下版本:

 // this code need to be placed on home page of tabbar for(UITabBarItem *item in self.tabBarController.tabBar.items) { item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } 

在iOS 10上方:

 // this need to be in appdelegate didFinishLaunchingWithOptions [[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]]; 

要更改制表符select颜色,而不是蓝色:

  1. selecttabItem。
  2. 从右侧菜单中的“显示身份检查员”。
  3. 用你喜欢的颜色设置“tintColor”属性。

在这里输入图像说明

相反,将它添加到每个UIViewController,你可以创build一个扩展,并改变UITabBarController的外观

更改未选中的图标颜色

 extension UITabBarController { override public func viewDidLoad() { super.viewDidLoad() tabBar.items?.forEach({ (item) -> () in item.image = item.selectedImage?.imageWithColor(UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal) }) } } 

更改选定的图标颜色

 let tabBarAppearance = UITabBar.appearance() tabBarAppearance.tintColor = UIColor.blackColor() 

更改(取消)所选标题颜色

 let tabBarItemApperance = UITabBarItem.appearance() tabBarItemApperance.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Edmondsans-Bold", size: 10)!, NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Normal) tabBarItemApperance.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Edmondsans-Bold", size: 10)!, NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Selected) 

UIImage扩展

 extension UIImage { func imageWithColor(color1: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) color1.setFill() let context = UIGraphicsGetCurrentContext() CGContextTranslateCTM(context!, 0, self.size.height) CGContextScaleCTM(context!, 1.0, -1.0); CGContextSetBlendMode(context!, .Normal) let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect CGContextClipToMask(context!, rect, self.CGImage!) CGContextFillRect(context!, rect) let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage UIGraphicsEndImageContext() return newImage } } 

没有通过仅使用appdelegate.m使用每个ViewController有一个更好的方法

在你的AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数,试试这个。

 UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; UITabBar *tabBar = tabBarController.tabBar; // repeat for every tab, but increment the index each time UITabBarItem *firstTab = [tabBar.items objectAtIndex:0]; // also repeat for every tab firstTab.image = [[UIImage imageNamed:@"someImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]; firstTab.selectedImage = [[UIImage imageNamed:@"someImageSelected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 

在Swift 3.0中,你可以这样写:

对于未选中的选项卡栏图像

 viewController.tabBarItem.image = UIImage(named: "image")?.withRenderingMode(.alwaysOriginal) 

对于选定的标签栏图像

 viewController.tabBarItem.selectedImage = UIImage(named: "image")?.withRenderingMode(.alwaysOriginal) 

我认为@安卡的答案是相当不错的,我也添加了以下代码,以突出显示的项目的颜色:

  let image = UIImage(named:"tab-account")!.imageWithRenderingMode(.AlwaysTemplate) let item = tabBar.items![IC.const.tab_account] as! UITabBarItem item.selectedImage = image 

或者在一行中:

  (tabBar.items![IC.const.tab_account] as! UITabBarItem).selectedImage = UIImage(named:"tab-account")!.imageWithRenderingMode(.AlwaysTemplate) 

所以它看起来像:

的TabBar

你只需要把这个代码放到你的第一个ViewController中,调用TabBarViewController(ViewDidload):

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self loadIconsTabBar]; } -(void) loadIconsTabBar{ UITabBar *tabBar = self.tabBarController.tabBar; // UITabBarItem *firstTab = [tabBar.items objectAtIndex:0]; UITabBarItem *secondTab = [tabBar.items objectAtIndex:1]; firstTab.image = [[UIImage imageNamed:@"icono1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]; firstTab.selectedImage = [[UIImage imageNamed:@"icono1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; secondTab.image = [[UIImage imageNamed:@"icono2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]; secondTab.selectedImage = [[UIImage imageNamed:@"icono2.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; } 

使用Swift 3编写iOS 10或更高版本的新答案是使用unselectedItemTintColor API。 例如,如果你已经初始化你的AppDelegate标签栏控制器,它看起来像下面这样:

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { ... let firstViewController = VC1() let secondViewController = VC2() let thirdViewController = VC3() let tabBarCtrl = UITabBarController() tabBarCtrl.viewControllers = [firstViewController, secondViewController, thirdViewController] // set the color of the active tab tabBarCtrl.tabBar.tintColor = UIColor.white // set the color of the inactive tabs tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray // set the text color ... } 

并设置选定和未选中的文本颜色:

 let unselectedItem = [NSForegroundColorAttributeName: UIColor.green] let selectedItem = [NSForegroundColorAttributeName: UIColor.red] self.tabBarItem.setTitleTextAttributes(unselectedItem, for: .normal) self.tabBarItem.setTitleTextAttributes(selectedItem, for: .selected) 

而不是在每个viewController中为tabBarItem添加渲染图像代码,请使用扩展名

 extension UITabBar{ func inActiveTintColor() { if let items = items{ for item in items{ item.image = item.image?.withRenderingMode(.alwaysOriginal) item.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.green], for: .normal) item.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected) } } } } 

然后在你的UITabBarController类中调用它

 class CustomTabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() tabBar.inActiveTintColor() } } 

你会得到如下输出: 在这里输入图像说明 注意:不要忘记将CustomTabBarViewController类分配到故事板中的TabBarController。

你只需要把这个代码放在你的appDelegate.m中(didFinishLaunchingWithOptions):

 [UITabBarItem.appearance setTitleTextAttributes: @{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateNormal]; [UITabBarItem.appearance setTitleTextAttributes: @{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected]; [[UITabBar appearance] setTintColor:[UIColor whiteColor]]; // for unselected items that are gray [[UITabBar appearance] setUnselectedItemTintColor:[UIColor whiteColor]]; 

改变“选定”选项卡栏项目颜色的最佳方法是在appdelegate didFinishLaunchingWithOptions方法上添加一个代码

 UITabBar.appearance().tintColor = UIColor(red: 242/255.0, green: 32/255.0, blue: 80/255.0, alpha: 1.0) 

它将更改所选项目的文字颜色

您可以通过界面构build​​器完成所有工作,查看此答案,它显示了如何同时处理活动和非活动状态, “更改标签栏项目在故事板中select的颜色”

对于迅捷3:

  // both have to declare in view hierarchy method //(ie: viewdidload, viewwillappear) according to your need. //this one is, when tab bar is selected self.tabBarItem.selectedImage = UIImage.init(named: "iOS")?.withRenderingMode(.alwaysOriginal) // this one is when tab bar is not selected self.tabBarItem.image = UIImage.init(named: "otp")?.withRenderingMode(.alwaysOriginal) 

这对我来说SWIFT 3

 viewConroller.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "image")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "image")) 

Swift解决scheme:对于UNSELECED项目:在每个ViewController – > ViewDidLoad()

 self.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "PHOTO_NAME")?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: UIImage(named: "NAME"))