使用swift Xcode 6的默认标签栏项目颜色 [英] Default tab bar item colors using swift Xcode 6
问题描述
环境:
- Xcode 6 beta 4
- Swift语言
- iOS标签应用程序(默认xCode项目)
Environment: - Xcode 6 beta 4 - Swift language - iOS Tabbed Application (default xCode project)
如何我可以将标签的默认灰色更改为其他内容吗? (最好全局)
How can I change the default grey color of the tabs to something else? (Preferably globally)
就我的研究而言,我需要以某种方式将每个标签的图像渲染模式更改为原始渲染模式但是我不知道如何
As far as my research goes I need to somehow change the image rendering mode for each tab to Original rendering mode however I don't know how
推荐答案
每个(默认)标签栏项目由文本和图标组成。通过指定外观来全局更改文本颜色非常容易:
Each (default) tab bar item consists of text and icon. It is pretty easy to change the text colors globally by specifying the appearance:
// 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类中重新定义它们。在下面的代码中添加代码 viewDidLoad
TabBarController
类的方法:
With images situation is a little bit more complicated. You cannot define their appearance globally. You should redefine them in your TabBarController class. Add code bellow to viewDidLoad
method of your TabBarController
class:
for item in self.tabBar.items as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
As我们知道UIImage类中没有 imageWithColor(...)
方法。所以这是扩展实现:
As we know there is no imageWithColor(...)
method in UIImage class. So here is the extension implementation:
// 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
}
}
<$ c从这个答案中借用了$ c> imageWithColor : https://stackoverflow.com/a/24545102/3050466
这篇关于使用swift Xcode 6的默认标签栏项目颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!