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.

3

3 Answers 3

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()
1
  • the code for Default tint color has no effect, and the second code, for the selected tab color, works well, but the function is deprecated Jul 22, 2016 at 7:41
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;
}
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.