默认的标签栏项目颜色使用Swift Xcode 6

环境: – Xcode 6testing版4 – Swift语言 – iOS选项卡式应用程序(默认xCode项目)

如何将标签的默认灰色更改为其他内容? (最好全球)

至于我的研究,我需要以某种方式改变每个选项卡的图像呈现模式原始呈现模式,但我不知道如何

每个(默认)标签栏项目由文本和图标组成。 通过指定外观来全局更改文本颜色非常简单:

// you can add this code to you AppDelegate application:didFinishLaunchingWithOptions: // or add it to viewDidLoad method of your TabBarController class UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected) 

与图像情况有点复杂。 你不能全局定义它们的外观。 你应该在你的TabBarController类中重新定义它们。 将代码添加到TabBarController类的viewDidLoad方法中:

 for item in self.tabBar.items as [UITabBarItem] { if let image = item.image { item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal) } } 

我们知道在UIImage类中没有imageWithColor(...)方法。 所以这里是扩展实现:

 // Add anywhere in your app extension UIImage { func imageWithColor(tintColor: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) let context = UIGraphicsGetCurrentContext() as CGContextRef 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) tintColor.setFill() CGContextFillRect(context, rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage UIGraphicsEndImageContext() return newImage } } 

imageWithColor是从这个答案借来的: https : imageWithColor

我没有足够的评论评论的声誉,但很多人有兴趣如何改变所选图像的颜色

只要添加另一个, if let后检查

 if let image = item.image 

像这样:

 if let selectedImage = item.selectedImage { item.selectedImage = selectedImage.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal) } 

这完美地解决了这个问题。 还有一点,因为你需要Swift 1.2和Xcode 6.3.2

 for item in self.tabBar.items as! [UITabBarItem] 

代替

 for item in self.tabBar.items as [UITabBarItem] 

希望有所帮助!

Swift 2.0

要更改标签栏图像的默认颜色,请将代码添加到TabBarController类的viewDidLoad方法中:

 for item in self.tabBar.items! as [UITabBarItem] { if let image = item.image { item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal) } } 

更新imageWithColor扩展。 与上面的方法一起使用,应该放在TabBarController类之外:

 extension UIImage { func imageWithColor(tintColor: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) let context = UIGraphicsGetCurrentContext()! as CGContextRef 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) tintColor.setFill() CGContextFillRect(context, rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage UIGraphicsEndImageContext() return newImage } } 

文字变色的方式没有变化,仅供参考。 也应该添加下面的代码viewDidLoad

 // you can add this code to you AppDelegate application:didFinishLaunchingWithOptions: // or add it to viewDidLoad method of your TabBarController class UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected) 

Swift 3.0

要更改标签栏图像的默认颜色,请将代码添加到TabBarController类的viewDidLoad方法中:

  for item in self.tabBar.items! as [UITabBarItem] { if let image = item.image { item.image = image.imageWithColor(tintColor: UIColor.yellow).withRenderingMode(.alwaysOriginal) } } 

更新imageWithColor扩展。 与上面的方法一起使用,应该放在TabBarController类之外:

 extension UIImage { func imageWithColor(tintColor: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) let context = UIGraphicsGetCurrentContext()! as CGContext context.translateBy(x: 0, y: self.size.height) context.scaleBy(x: 1.0, y: -1.0); context.setBlendMode(CGBlendMode.normal) let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) context.clip(to: rect, mask: self.cgImage!) tintColor.setFill() context.fill(rect) let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage UIGraphicsEndImageContext() return newImage } }