Good day,
I'm using the UITabBarController from the storyboard, and I want to change the icons colors from the default which is gray to white.
I tried many solution is found here in stackOverflow, and in other websites but all was useless.
Good day,
I'm using the UITabBarController from the storyboard, and I want to change the icons colors from the default which is gray to white.
I tried many solution is found here in stackOverflow, and in other websites but all was useless.
Default Tab Tint Color:-
Obj-C Version:-
[[self tabBar] setTintColor:[UIColor blackColor]];
Swift 2.2 Version:-
self.tabBar.tintColor = UIColor.whiteColor()
Selected Tab Tint Color:-
Obj-C Version:-
[[self tabBar] setSelectedImageTintColor:[UIColor blueColor]];
Swift2.2 Version:-
self.tabBar.selectedImageTintColor = UIColor.blueColor()
I found the solution it is was to use this loop
for(UITabBarItem *item in self.tabBar.items) {
// use the UIImage category code for the imageWithColor: method
item.image = [[[item selectedImage] imageWithColor:[UIColor redColor]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
in the UITabBarController
class.
and should add the UIImage+Overlay
which contains the method imageWithColor
- (UIImage *)imageWithColor:(UIColor *)color1
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
[color1 setFill];
CGContextFillRect(context, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I've found another solution from this link. It is not necessary to use loop. I modified his code to remove warnings
makeImageWithColorAndSize function can be implemented as a normal function
extension UIImage {
func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Example of use:
override func viewWillAppear(animated: Bool) {
self.tabBar.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(
UIColor.blueColor(),
size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))
super.viewWillAppear(animated)
}
original code of Gwendle