未选中的UITabBar颜色?

我有一个UITabBar 5项。 我想更改所有项目的未选中的颜色。 这些项目没有在UIViewController类中声明(我构build它们并链接了Storyboard中的视图)。

有没有这样的代码: [[UITabBar appearance] set***UN***SelectedImageTintColor:[UIColor whiteColor]];

所以说,我不能删除接受的答案(我试过),但显然,有很多upvotes评论,这不适用于iOS 7。

看到下面的其他答案,还有更多upvotes,或@ Liam评论这个答案的链接。


仅适用于iOS 6

它应该像这样简单:

 [[UITabBar appearance] setTintColor:[UIColor grayColor]]; // for unselected items that are gray [[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]]; // for selected items that are green 

据我所知,这在iOS 7下不起作用。 特别是,标签栏的tintColor将定义所选标签的颜色,而不是未选中标签的颜色。 如果要更改iOS 7中的默认设置,则似乎必须实际使用不同的图标(以您喜欢的颜色为未选中的选项卡)并设置文本的颜色。

这个例子应该将选中的选项卡染成绿色,并呈现其他颜色。 在你的TabBarController中运行这段代码:

 // set color of selected icons and text to red self.tabBar.tintColor = [UIColor redColor]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]; // set color of unselected text to green [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; // set selected and unselected icons UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0]; // this way, the icon gets rendered as it is (thus, it needs to be green in this example) item0.image = [[UIImage imageNamed:@"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor item0.selectedImage = [UIImage imageNamed:@"selected-icon.png"]; 

如果仅在故事板中设置图标,则只能控制选定选项卡的颜色(tintColor)。 所有其他图标和相应的文字将以灰色绘制。

也许有人知道更简单的方法来采用iOS 7下的颜色?

为iOS 7扩展了@Sven Tiffe的答案,您可以让您的代码自动着色故事板中添加的未选中的UITabBar图像。 下面的方法将节省您不得不创build两组图标图像(即select与未选中),并不得不以编程方式加载它们。添加类别方法imageWithColor:(请参阅 – 如何更改iOS和WatchKit中的图像tintColor )到您的项目,然后把下面的你的自定义UITabBarController的viewDidLoad方法:

 // set the selected colors [self.tabBar setTintColor:[UIColor whiteColor]]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]; UIColor * unselectedColor = [UIColor colorWithRed:184/255.0f green:224/255.0f blue:242/255.0f alpha:1.0f]; // set color of unselected text [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:unselectedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; // generate a tinted unselected image based on image passed via the storyboard for(UITabBarItem *item in self.tabBar.items) { // use the UIImage category code for the imageWithColor: method item.image = [[item.selectedImage imageWithColor:unselectedColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } 

创build一个名为UIImage + Overlay的类别,并在UIImage + Overlay.m(从这个答案中提取):

 @implementation UIImage(Overlay) - (UIImage *)imageWithColor:(UIColor *)color1 { UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, self.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextSetBlendMode(context, kCGBlendModeNormal); CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); CGContextClipToMask(context, rect, self.CGImage); [color1 setFill]; CGContextFillRect(context, rect); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } @end 

这在iOS 10中得到修复,有三种可能的简单解决scheme:

  1. 代码实例(swift):

    self.tabBar.unselectedItemTintColor = unselectedcolor

  2. 来自IB的实例:

    添加一个关键path:unselectedItemTintColor

    types:颜色

  3. 全球外观(迅捷):

    UITabBar.appearance()。unselectedItemTintColor = unselectedcolor

将user3719695的答案翻译成Swift,现在使用扩展:

的UIImage + Overlay.swift

 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, CGBlendMode.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 } } 

customTabBar.swift

 override func viewDidLoad() { super.viewDidLoad() for item in self.tabBar.items! { item.image = item.selectedImage?.imageWithColor(unselectedColor).imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) //In case you wish to change the font color as well let attributes = [NSForegroundColorAttributeName: unselectedColor] item.setTitleTextAttributes(attributes, forState: UIControlState.Normal) } } 

我不得不将代码移到viewWillAppear因为在viewDidLoad中图像还没有设置。

Swift 3翻译

 import Foundation import UIKit extension UIImage { func with(color: UIColor) -> UIImage { guard let cgImage = self.cgImage else { return self } UIGraphicsBeginImageContextWithOptions(size, false, scale) let context = UIGraphicsGetCurrentContext()! context.translateBy(x: 0, y: size.height) context.scaleBy(x: 1.0, y: -1.0) context.setBlendMode(.normal) let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height) context.clip(to: imageRect, mask: cgImage) color.setFill() context.fill(imageRect) let newImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext(); return newImage } } class MYTabBarController: UITabBarController { let unselectedColor = UIColor(red: 108/255.0, green: 110/255.0, blue: 114/255.0, alpha: 1.0) let selectedColor = UIColor.blue() override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Unselected state colors for item in self.tabBar.items! { item.image = item.selectedImage!.with(color: unselectedColor).withRenderingMode(.alwaysOriginal) } UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : unselectedColor], for: .normal) // Selected state colors tabBar.tintColor = selectedColor UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : selectedColor], for: .selected) } } 

参考这里的答案: iOS 7中的UITabBar tint

您可以为选定的和未选中的选项卡栏button设置色调颜色,如下所示:

 [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]]; [[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]]; 

第一行设置未选中的颜色 – 在本例中为红色 – 通过设置UIView的tintColor,当它包含在标签栏中时。 请注意,这只会设置未select的图像的色调颜色 – 它不会更改它下面的文本的颜色。

第二行将选项卡栏的选定图像色调颜色设置为绿色。

从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 ... } 

Swift版本 –

 UITabBar.appearance().tintColor = UIColor.gray UITabBar.appearance().unselectedItemTintColor = UIColor.gray 

Swift 4版本(没有隐含地解开可选项):

的UIImage + Overlay.swift

 import UIKit extension UIImage { func with(color: UIColor) -> UIImage? { guard let cgImage = self.cgImage else { return self } UIGraphicsBeginImageContextWithOptions(size, false, scale) if let context = UIGraphicsGetCurrentContext() { context.translateBy(x: 0, y: size.height) context.scaleBy(x: 1.0, y: -1.0) context.setBlendMode(.normal) let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height) context.clip(to: imageRect, mask: cgImage) color.setFill() context.fill(imageRect) if let newImage = UIGraphicsGetImageFromCurrentImageContext() { UIGraphicsEndImageContext(); return newImage } } return nil; } } 

CustomTabBarController.swift

 class CustomTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() if #available(iOS 10.0, *) { self.tabBar.unselectedItemTintColor = UIColor.init(white: 1, alpha: 0.5) } else { // Fallback on earlier versions if let items = self.tabBar.items { let unselectedColor = UIColor.init(white: 1, alpha: 0.5) let selectedColor = UIColor.white // Unselected state colors for item in items { if let selectedImage = item.selectedImage?.with(color: unselectedColor)?.withRenderingMode(.alwaysOriginal) { item.image = selectedImage } } UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : unselectedColor], for: .normal) // Selected state colors tabBar.tintColor = selectedColor UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : selectedColor], for: .selected) } } UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "overpass-light", size: 12)!, NSAttributedStringKey.foregroundColor: UIColor.white], for: UIControlState.normal) } } 

@ JoeGalid的imageWithColor:解决scheme与Xamarin:

 using CoreGraphics; using UIKit; namespace Example { public static class UIImageExtensions { public static UIImage ImageWithColor(this UIImage image, UIColor color) { UIGraphics.BeginImageContextWithOptions(image.Size, false, image.CurrentScale); color.SetFill(); var context = UIGraphics.GetCurrentContext(); context.TranslateCTM(0, image.Size.Height); context.ScaleCTM(1.0f, -1.0f); context.SetBlendMode(CoreGraphics.CGBlendMode.Normal); var rect = new CGRect(0, 0, image.Size.Width, image.Size.Height); context.ClipToMask(rect, image.CGImage); context.FillRect(rect); var newImage = UIGraphics.GetImageFromCurrentImageContext() as UIImage; UIGraphics.EndImageContext(); return newImage; } } } 

然后在设置标签栏项目时使用它:

 var image = UIImage.FromBundle("name"); barItem.Image = image.ImageWithColor(UIColor.Gray).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); barItem.SelectedImage = image.ImageWithColor(UIColor.Red).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)