I'm trying to set the icon color and background color of an individual item on my TabBar within the TabBarController.
I have 5 icons on my TabBar, 4 of which i've set the color for, it's just this last icon that i'm struggling with. I've attached a link below of an image to show what i'm trying to achieve.
The specific icon is (the one in the middle of the image) for my camera feed, this icon i would like to be white and the background to be red. My code so far is such - On my TabBarController:
var imageNames = ["FeedIcon", "ExploreIcon", "CameraIcon", "ActivityIcon", "MyProfileIcon"]
let tabItems = tabBar.items as! [UITabBarItem]
for (index, value) in enumerate(tabItems)
{
var imageName = imageNames[index]
value.image = UIImage(named: imageName)
value.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0)
}
//for below i've used a extension for imageWithColor to set the color of my icons
for tabItems in self.tabBar.items as! [UITabBarItem] {
if let image = tabItems.image {
tabItems.image = image.imageWithColor(UIColor(red: 57.0/255.0, green: 57.0/255.0, blue: 57.0/255.0, alpha: 1.0)).imageWithRenderingMode(.AlwaysOriginal)
}
}
My Extension:
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, kCGBlendModeNormal)
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
}
}