I'm using tab bar and I have 2 problems with color.
1st Problem, the tint color is grey, I used some code to change it to white but it turn to white only when tab is pressed.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let barColor = UIColor(red: 49/255, green: 75/255, blue: 108/255, alpha: 1.0)
let pressedTintColor = UIColor.whiteColor()
UITabBar.appearance().barTintColor = barColor
UITabBar.appearance().tintColor = pressedTintColor
return true
}
2nd Problem, the background color of pressed tab is supposed to change but it's not changing.
This is how tab bar looks.
And this is how it's supposed to look.
(1st pic is in Xcode Simulator just as test, 2nd pic is design of it, so it's not important to much about images and text of tabs)
So it's supposed all tabs to be all the time white, and when a tab is pressed to change just background color of tab.
Use the Image Tint (selectedImageTintColor) field to specify the bar item’s tint color when that tab is selected. By default, that color is blue.
Swift 3 First of all, make sure you have added the BOOLEAN key "View controller-based status bar appearance" to Info.plist, and set the value to "NO". Appdelegate.swift Insert code somewhere after "launchOptions:[UIApplicationLaunchOptionsKey: Any]?) -> Bool {" Change the color of the tab bar itself with RGB color value:
Image setup- from the storyboard set Bar Item Image and Selected Image. To remove the tint overlay on the images go to Assets catalog, select the image and change its rendering mode like this: This will prevent the Tab bar component from setting its default image tint.
To remove the tint overlay on the images go to Assets catalog, select the image and change its rendering mode like this: This will prevent the Tab bar component from setting its default image tint.
To solve the problems in your AppDelegate
do this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().tintColor = UIColor.whiteColor()
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)
return true
}
And in your ViewController
do something like this:
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
}
}
extension UIImage {
func imageWithColor(tintColor: 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, CGBlendMode.Normal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
class FirstViewController: UIViewController {
var tabBar: UITabBar?
override func viewDidLoad() {
super.viewDidLoad()
tabBar = self.tabBarController!.tabBar
tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height))
// To change tintColor for unselected tabs
for item in tabBar!.items! as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
*The first extension
to UIImage
is taken from another question by the same author: How to change default grey color of tab bar items?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With