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())
Sam_M
source share