Default Tab Bar Item Colors Using Swift Xcode 6

Default tab bar item colors using swift Xcode 6

Each (default) tab bar item consists of text and icon. It is pretty easy to change the text colors globally by specifying the appearance:

// you can add this code to you AppDelegate application:didFinishLaunchingWithOptions: 
// or add it to viewDidLoad method of your TabBarController class
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)

With images situation is a little bit more complicated. You cannot define their appearance globally. You should redefine them in your TabBarController class. Add code bellow to viewDidLoad method of your TabBarController class:

for item in self.tabBar.items as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}

As we know there is no imageWithColor(...) method in UIImage class. So here is the extension implementation:

// Add anywhere in your app
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, .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
}
}

imageWithColor was borrowed from this answer: https://stackoverflow.com/a/24545102/3050466

Changing tab bar item image and text color iOS

From UITabBarItem class docs:

By default, the actual unselected and selected images are
automatically created from the alpha values in the source images. To
prevent system coloring, provide images with
UIImageRenderingModeAlwaysOriginal.

The clue is not whether you use UIImageRenderingModeAlwaysOriginal, the important thing is when to use it.

To prevent the grey color for unselected items, you will just need to prevent the system colouring for the unselected image. Here is how to do this:

var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem

As you can see, I asked iOS to apply the original color (white, yellow, red, whatever) of the image ONLY for the UNSELECTED state, and leave the image as it is for the SELECTED state.

Also, you may need to add a tint color for the tab bar in order to apply a different color for the SELECTED state (instead of the default iOS blue color). As per your screenshot above, you are applying white color for the selected state:

self.tabBar.tintColor = UIColor.whiteColor()

EDIT:

Sample Image

Change tab bar item selected color in a storyboard

Add Runtime Color attribute named "tintColor" from StoryBoard. This is working(for Xcode 8 and above).

if you want unselected color.. you can add unselectedItemTintColor too.

setting tintColor as Runtime Attribute

How to change default grey color of tab bar items?

The problem in your code that you create UITabBar object like let tabBar = UITabBar() and this object has no relation to the tabs which are located on the form. Your tabBar is a new empty object that contains no one UITabBarItem objects and when you call this:

UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/CGFloat(tabBar.items!.count), tabBar.frame.height))

the error occurs when you try to do this: tabBar.items!.count. You're trying to unwrap optional items array [UITabBarItem]? and it nil becouse tabBar is empty object and have no items.

To fix this you need to get reference to UITabBar from current UITabBarController for example like this:

class SecondViewController: 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))
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

How do you change the color of unselected items in a Tab Bar?

Make the icon images Black, if they are white Im not sure it will work. As in the actual image themselves should be black in the assets folder not white.

If you add a tabbar from the story board you can put these line of code in that method in the appDelegate method shown in your post

UITabBar.appearance().barTintColor = UIColor.black
UITabBar.appearance().tintColor = UIColor.red
UITabBar.appearance().unselectedItemTintColor = .white

Tab Bar Items change colors

Yes, to achieve it you have to options depending on your setup, in both cases you have to implement the following method func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem):

  • If you are using a ViewController, it will have to implement the UITabBarController and UITabBarControllerDelegate. You will have to set the delegate property to self, you have to do it in the viewDidLoad method:

    self.delegate = self

  • If you are using a Tab Bar Controller Scene in your Storybard), create an instance of a UITabBarController class, in my example TabViewController, and set it as a custom class in your Tab View Controller.

Sample Image

Next, in both cases you will have to implement the following method:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) 
self.tabBar.tintColor = UIColor.red
}

UITabBar bar item icons are gray by default?

In my first view controller's viewDidLoad, I placed the following and it worked like a charm:

    let aTabArray: [UITabBarItem] = (self.tabBarController?.tabBar.items)!

for item in aTabArray {
item.image = item.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
item.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0)
}


Related Topics



Leave a reply



Submit