更改标签栏项目图像和文本颜色的iOS

这是我的标签栏:

在这里输入图像说明

下图显示正在运行的程序和所选的“新闻”项目:

在这里输入图像说明

很明显,酒吧的色调工作正常,因为我想!

但tintColor只影响图像,而不是文字。

此外,当一个项目被选中(如上所示,新闻)项目的颜色变蓝! 我如何防止这种情况发生? 我希望它保持白色。

为什么select时文本会变成白色,而不是当它被取消时变成白色?

我基本上希望项目的颜色和文字颜色一直是白色的。

我如何做到这一点? 谢谢你的帮助。

它是否需要每个单独项目的快速代码?

编辑:

在这里输入图像说明

从UITabBarItem类文档:

默认情况下,实际未选中和选定的图像是从源图像中的alpha值自动创build的。 为了防止系统着色,请使用UIImageRenderingModeAlwaysOriginal提供图像。

线索不是你使用UIImageRenderingModeAlwaysOriginal,重要的是什么时候使用它。

要防止未选定项目的灰色,您只需要防止未选定图像的系统着色。 这是如何做到这一点:

var firstViewController:UIViewController = UIViewController() // The following statement is what you need var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME")) firstViewController.tabBarItem = customTabBarItem 

正如你所看到的,我要求iOS只将图像的原始颜色(白色,黄色,红色,任何)应用于UNSELECTED状态,并将图像保持为SELECTED状态。

此外,您可能需要为选项卡栏添加色调颜色,以便为SELECTED状态(而不是默认的iOS蓝色)应用不同的颜色。 按照上面的屏幕截图,您将为所选状态应用白色:

 self.tabBar.tintColor = UIColor.whiteColor() 

编辑:

在这里输入图像说明

Swift 3

我通过创build一个自定义的tabbar控制器,并在viewDidLoad方法中添加此代码。

  if let count = self.tabBar.items?.count { for i in 0...(count-1) { let imageNameForSelectedState = arrayOfImageNameForSelectedState[i] let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i] self.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal) self.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal) } } let selectedColor = UIColor(red: 246.0/255.0, green: 155.0/255.0, blue: 13.0/255.0, alpha: 1.0) let unselectedColor = UIColor(red: 16.0/255.0, green: 224.0/255.0, blue: 223.0/255.0, alpha: 1.0) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: unselectedColor], for: .normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor], for: .selected) 

它为我工作!

在这里输入图像说明

Swift编程:

对于图像:

 custom.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "tab_icon_normal"), selectedImage: UIImage(named: "tab_icon_seelcted")) 

ForText:

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState:.Normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected) 

Swift 3

这工作对我来说(指设置tabBarItems图像的颜色):

 UITabBar.appearance().tintColor = ThemeColor.Blue if let items = tabBarController.tabBar.items { let tabBarImages = getTabBarImages() // tabBarImages: [UIImage] for i in 0..<items.count { let tabBarItem = items[i] let tabBarImage = tabBarImages[i] tabBarItem.image = tabBarImage.withRenderingMode(.alwaysOriginal) tabBarItem.selectedImage = tabBarImage } } 

我注意到,如果你设置渲染模式= .alwaysOriginal的图像,UITabBar.tintColor没有任何作用。

Swift 3

首先,确保已经在Info.plist中添加了BOOLEAN键“基于视图控制器的状态栏外观”,并将该值设置为“NO”。

Appdelegate.swift

在“launchOptions:[UIApplicationLaunchOptionsKey:Any]”之后的某处插入代码?) – > Bool {

  1. 使用RGB颜色值更改标签栏本身的颜色:

UITabBar.appearance().barTintColor = UIColor(red: 0.145, green: 0.592, blue: 0.804, alpha: 1.00)

或者默认的UI颜色之一:

UITabBar.appearance().barTintColor = UIColor.white)


  1. 更改选项卡项目的文本颜色:

所选项目

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected)

不活动的项目

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.black], for: .normal)

  1. 要改变图像的颜色,我相信最简单的方法是分离图像,每个状态一个。

如果不从头开始制作图标,交替的黑白版本在Photoshop中相对容易制作。


Adobe Photoshop(几乎任何版本都可以)

确保你的图标图像有透明的背景,图标本身是纯黑色(或closures)。

打开图像文件,将其保存在不同的文件名下(例如exampleFilename-Inverted.png)

在“图像”菜单的“调整”子菜单中:

点击“反转”

你现在有你的原始图标的否定。

在XCode中,将其中一个图像设置为故事板中选项卡栏属性下的“Selected Image”,并在“Bar Item”图像下指定“inactive”版本。

塔达🍺

尝试添加AppDelegate.swift (在应用程序的方法):

 UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0) // For WHITE color: UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) 

例:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Tab bar icon selected color UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0) // For WHITE color: UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) return true } 

例:

在这里输入图像说明

在这里输入图像说明

我的英语太糟糕了! 对不起! 🙂

Swift 3.0

我创build了tabbar类文件并编写了下面的代码

viewDidLoad

 self.tabBar.barTintColor = UIColor.white self.tabBar.isTranslucent = true let selectedColor = UIColor.red let unselectedColor = UIColor.cyan UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: unselectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .selected) if let items = self.tabBar.items { for item in items { if let image = item.image { item.image = image.withRenderingMode( .alwaysOriginal ) item.selectedImage = UIImage(named: "(Imagename)-a")?.withRenderingMode(.alwaysOriginal) } } } 

viewDidLoad之后:

  override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { if(item.title! == "title") { item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal) } if(item.title! == "title") { item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal) } if(item.title! == "title") { item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal) } if(item.title! == "title") { item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal) } if(item.title! == "title") { item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal) } } 

在视图加载方法,你必须设置选定的图像和其他图像显示与RenderingMode和在标签栏委托方法您设置选定的图像作为标题

你可以设置UIBarItem的tintColor:

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected) 

从这里 。

每个标签栏项目都有一个标题,选定的图像,未选中的图像和一个徽章值。

使用图像色调(selectedImageTintColor)字段指定选中该选项卡时的条形色彩的色调颜色。 默认情况下,该颜色是蓝色的。

只需在项目中添加一个新的UITabBarController引用。接下来在这个控制器中创build一个UITabBar的引用:

 @IBOutlet weak var appTabBar: UITabBar! 

viewDidLoad()中 ,只需在标题文字颜色下面添加如下内容

  appTabBar.tintColor = UIColor.scandidThemeColor() 

对于图像

  tabBarItem = UITabBarItem(title: "FirstTab", image: UIImage(named: "firstImage"), selectedImage: UIImage(named: "firstSelectedImage"))