Default tab bar item colors using swift Xcode 6

前端 未结 4 619
执念已碎
执念已碎 2020-12-01 00:59

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

4条回答
  •  星月不相逢
    2020-12-01 01:42

    Swift 3.0

    To change the default color for tab bar images, 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(tintColor: UIColor.yellow).withRenderingMode(.alwaysOriginal)
            }
        }
    

    Update the imageWithColor extension. Used with the above method and should be placed outside of your TabBarController class:

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

提交回复
热议问题