For me the best way is changing the image color.
func imageWithColor(_ color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: rect, mask: cgImage!)
color.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
newImage.accessibilityIdentifier = accessibilityIdentifier
return newImage
}
then, you can update the tabBarItem's properties such as image and selectedImage:
func setupColorAttributes(to item: UITabBarItem) {
item.image = item.image?.imageWithColor(.white)?.withRenderingMode(.alwaysOriginal)
item.selectedImage = item.image?.imageWithColor(.highLight)?.withRenderingMode(.alwaysOriginal)
}