环境:-Xcode 6 beta 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类中重新定义它们。将下面的代码添加到viewDidLoad您的TabBarController类的方法中:
viewDidLoad
TabBarController
for item in self.tabBar.items as [UITabBarItem] { if let image = item.image { item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal) } }
众所周知imageWithColor(...),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 } }