Last active
January 25, 2024 11:35
-
-
Save aboodmufti/854176905455c95c10b3 to your computer and use it in GitHub Desktop.
How to customize iOS tab icon colors (2 ways)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
//change text under icon | |
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.init(netHex: 0x000000)], forState: .Normal) | |
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.init(netHex: 0xffffff)], forState: .Selected) | |
//store every image in a variable | |
let homeUnselectedImage: UIImage = UIImage(named: "home_grey")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) | |
let homeSelectedImage: UIImage = UIImage(named: "home_white")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) | |
let userUnselectedImage: UIImage = UIImage(named: "user_grey")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) | |
let userSelectedImage: UIImage = UIImage(named: "user_white")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) | |
let globeUnselectedImage: UIImage = UIImage(named: "globe_grey")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) | |
let globeSelectedImage: UIImage = UIImage(named: "globe_white")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) | |
//use that variable for each icon | |
tabBar.items![0].image = homeUnselectedImage | |
tabBar.items![0].selectedImage = homeSelectedImage | |
tabBar.items![1].image = globeUnselectedImage | |
tabBar.items![1].selectedImage = globeSelectedImage | |
tabBar.items![2].image = userUnselectedImage | |
tabBar.items![2].selectedImage = userSelectedImage | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
//Setting the text color under each tab bar item | |
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState:.Normal) | |
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Selected) | |
//setting tab bar items colors when NOT selected | |
for item in self.tabBar.items as [UITabBarItem]! { | |
if let image = item.image { | |
item.image = image.imageWithColor(UIColor.blackColor()).imageWithRenderingMode(.AlwaysOriginal) | |
} | |
} | |
//setting tab bar items colors when selected | |
UITabBar.appearance().tintColor = UIColor.whiteColor() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This part is not my code, I took it from : http://stackoverflow.com/a/24545102/3050466 | |
//You will need this code to be able to do the second way (TabViewController2.swift) | |
extension UIImage { | |
func imageWithColor(color1: UIColor) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) | |
color1.setFill() | |
let context = UIGraphicsGetCurrentContext() as CGContextRef! | |
CGContextTranslateCTM(context, 0, self.size.height) | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGContextSetBlendMode(context, CGBlendMode.Normal) | |
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect | |
CGContextClipToMask(context, rect, self.CGImage) | |
CGContextFillRect(context, rect) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage | |
UIGraphicsEndImageContext() | |
return newImage | |
} | |
} |
@chienpm304,
hi
Are you able to find a solution for setting colours for a specific item and other sets of different colours?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how could I set color for a specific item?