1

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. enter image description here

I tried many solution is found here in stackOverflow, and in other websites but all was useless.

Ahmed Adnan Qazzaz
  • 333
  • 1
  • 3
  • 15

3 Answers3

0

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()
Meet
  • 1,196
  • 12
  • 26
0

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;
}
Ahmed Adnan Qazzaz
  • 333
  • 1
  • 3
  • 15
0

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

Community
  • 1
  • 1
David
  • 1,152
  • 16
  • 24