Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color for tab bar non selected icon in swift?

How to change color for tab bar non selected icon and text? I found this answer (How to change inactive icon/text color on tab bar?), but can't implement it for swift.

like image 309
Walter West Avatar asked Apr 26 '15 10:04

Walter West


6 Answers

iOS 10

class TabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // make unselected icons white
        self.tabBar.unselectedItemTintColor = UIColor.white
    }
}
like image 130
Derek Soike Avatar answered Oct 03 '22 21:10

Derek Soike


In iOS 11 you can set the property directly at UIToolBar in storyboard:

unselectedItemTintColor | Color | [desired color]

Xcode print

Screen Shot

like image 44
Phelippe Amorim Avatar answered Oct 03 '22 20:10

Phelippe Amorim


The below sets the defaults for all UITabBarItem's, you can add it to your AppDelegate. It will change your text color.

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blackColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.whiteColor()}, forState:.Normal)

For changing the icon' color you can either set the image for the given state where your image already have the good color.

self.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectedImage"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.tabBarItem.image = [[UIImage imageNamed:@"notSelectedImage"] 
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Or you can do it this way :

Add an extension to UIImage class (from this answer) :

extension UIImage {
func imageWithColor(color1: 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)
    color1.setFill()
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
}
}

And in your viewDidLoad :

for item in self.tabBar.items as [UITabBarItem] {
    if let image = item.image {
        item.image = image.imageWithColor(UIColor.blackColor()).imageWithRenderingMode(.AlwaysOriginal)
    }
}
like image 23
BoilingLime Avatar answered Oct 03 '22 19:10

BoilingLime


Swift 4+

UITabBar.appearance().unselectedItemTintColor = UIColor.green

Use this code in appDelegate's didFinishLaunchingWithOptions method.

like image 37
Abhishek Jain Avatar answered Oct 03 '22 21:10

Abhishek Jain


Complementing @BoilingLime's answer, here it goes the second alternative's UIImage extension in Swift 3:

extension UIImage {
func imageWithColor(color1: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

    let context = UIGraphicsGetCurrentContext()! as CGContext
    context.translateBy(x: 0, y: self.size.height)
    context.scaleBy(x: 1.0, y: -1.0);
    context.setBlendMode(CGBlendMode.normal)

    let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
    context.clip(to: rect, mask: self.cgImage!)
    color1.setFill()
    context.fill(rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
    UIGraphicsEndImageContext()

    return newImage
}
}
like image 23
Bruno Barbosa Avatar answered Oct 03 '22 20:10

Bruno Barbosa


If you are looking for an iOS 11 swift 4 solution, do something like this in the appDelegate. This is changing all the unselected tab bar items to black.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UITabBar.appearance().unselectedItemTintColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 1)

    return true
}
like image 43
thalacker Avatar answered Oct 03 '22 20:10

thalacker