Tab bar color in Swift 3?

In Swift 2, I used the Runtime User Defined Runtime attribute in the Storyboard with the tintColor key path to change the colors of the tab bar item icons. However, it looks like tintColor was removed using Swift 3. How do I change the selected color of the tab bar items in the tab bar controller in Swift 3?

Thanks!

EDIT: screenshot attached

enter image description here

+8
ios swift swift3 storyboard uitabbarcontroller
source share
2 answers

Use tabBarItem.setTitleTextAttributes to change the text color of individual panel elements.
Put this in the viewDidLoad method for each tab:

 self.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red()], for:.selected) 

To change the icon color and text color, a simple solution is to change the tabBar hue color in viewWillAppear mode for each tab:

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tabBarController?.tabBar.tintColor = UIColor.red() } 

Another solution to change the hue of an image is to create an extension for UIImage and use it to change the selected image using a custom hue:

 extension UIImage { func tabBarImageWithCustomTint(tintColor: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) let context: CGContext = UIGraphicsGetCurrentContext()! context.translate(x: 0, y: self.size.height) context.scale(x: 1.0, y: -1.0) context.setBlendMode(CGBlendMode.normal) let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) context.clipToMask(rect, mask: self.cgImage!) tintColor.setFill() context.fill(rect) var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() newImage = newImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal) return newImage } } 

Use this code to change the selected image:

 self.tabBarItem.selectedImage = self.tabBarItem.selectedImage?.tabBarImageWithCustomTint(tintColor: UIColor.red()) 
+10
source share

The last code as a parameter to Swift 3 is

 extension UIImage { func tabBarImageWithCustomTint(tintColor: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) let context: CGContext = UIGraphicsGetCurrentContext()! context.translateBy(x: 0, y: self.size.height) context.scaleBy(x: 1.0, y: -1.0) context.setBlendMode(CGBlendMode.normal) let rect: CGRect = 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) var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() newImage = newImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal) return newImage } } 
0
source share

All Articles