200

I want to change my tab bar items to be pink when selected instead of the default blue.

How can i accomplish this using the storyboard editor in Xcode 6?

Here are my current setting which are not working, the blue background works but the pink doesnt work:

enter image description here

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
Deekor
  • 9,144
  • 16
  • 69
  • 121
  • Possible duplicate of [tab-bar-item-tint-color](http://stackoverflow.com/questions/21361101/tab-bar-item-tint-color)! – User31 Nov 10 '14 at 07:40
  • Check this answer it will help you for the solution: https://stackoverflow.com/a/58727092/7804300 – Talha Ahmed Nov 15 '19 at 12:44

15 Answers15

442

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

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • 26
    it worked for me when I added this property to UITabBarItem and not UITabBar. – Sourabh86 Jun 05 '15 at 06:49
  • Any reason why it doesn't work with the option attribute ? – klefevre Jul 31 '15 at 08:08
  • Xcode bug, nothing else. I have also found too many bugs of using UITextView inside UITableViewCell using from storyboard in Xcode 6.4. – Mehul Thakkar Jul 31 '15 at 09:12
  • Actually, value of any property can be set from User Defined Attributes, so, instead of programmatically setting tintColor property, i used to set directly from xib – Mehul Thakkar Sep 07 '15 at 04:58
  • @MehulThakkar how can I do this with the font size of Title of the item ? – Asadullah Ali Sep 14 '15 at 09:40
  • @AsadullahAli: Not getting, what you are asking for – Mehul Thakkar Sep 14 '15 at 11:10
  • I want to change the font size of the Title of TabBarItem. Can I do it from storyboard they way you changed the TintColor. – Asadullah Ali Sep 14 '15 at 11:15
  • No, you cant, you can only set properties of given UI-Object, now, UITabbarItem doesn't have any property for font size – Mehul Thakkar Sep 15 '15 at 05:34
  • you can change font size by programmatically writing following code `[tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil] forState:UIControlStateNormal];` – Mehul Thakkar Sep 15 '15 at 05:35
  • 1
    But how do you set the tab bar item UNselected color in storyboard? – Peter Mar 11 '16 at 16:44
  • 1
    Works perfect for my case - I need my five tabBar items to be unique colours when selected. I was able to set this Runtime Attribute on the `UITabBarItem`, on the `UINavigationController` connected to my tabBar and it works flawlessly. All without writing any code too which is great, as I am reusing a VC class three times. Thanks! – rosshump Jul 12 '16 at 19:31
  • Had the same problem in xcode 7.3.1 so if its a bug its still unresolved... – Felipe Ignacio Noriega Alcaraz Jul 25 '16 at 13:51
  • 7
    conversely - this is the only approach that works on iOS 10 (tint from attributes panel seems to not work, and runtime attribute "tintColor" of uitabbaritem doesn't work too. – luky Oct 11 '16 at 14:35
  • 1
    Worked well on iOS 10. – Mohammad Zaid Pathan Jun 10 '17 at 09:48
  • `UITabBar.appearance().tintColor = #colorLiteral(red: 0.9490196078, green: 0.3647058824, blue: 0.1450980392, alpha: 1)` work for me ... – Wahab Khan Jadon Aug 15 '20 at 17:08
201

This elegant solution works great on SWIFT 3.0, SWIFT 4.2 and SWIFT 5.1:

On the Storyboard:

  1. Select your Tab Bar
  2. Set a Runtime Attibute called tintColor for the desired color of the Selected Icon on the tab bar
  3. Set a Runtime Attibute called unselectedItemTintColor for the desired color of the Unselected Icon on the tab bar

enter image description here

Edit: Working with Xcode 8/10, for iOS 10/12 and above.

Marcelo Gracietti
  • 3,121
  • 1
  • 16
  • 24
70

In Swift, using xcode 7 (and later), you can add the following to your AppDelegate.swift file:

UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

This is the what the complete method looks like:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // I added this line
    UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

    return true
}

In the example above my item will be white. The "/255.0" is needed because it expects a value from 0 to 1. For white, I could have just used 1. But for other color you'll probably be using RGB values.

Jarrod
  • 9,349
  • 5
  • 58
  • 73
56

On Xcode8 I have changed the ImageTint from the storyboard and it works well.

enter image description here

The result:

enter image description here

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
21

Swift 3 | Xcode 10

If you want to make all tab bar items the same color (selected & unselected)...


Step 1

Make sure your image assets are setup to Render As = Template Image. This allows them to inherit color.

Xcode Assets


Step 2

Use the storyboard editor to change your tab bar settings as follows:

  • Set Tab Bar: Image Tint to the color you want the selected icon to inherit.
  • Set Tab Bar: Bar Tint to the color you want the tab bar to be.
  • Set View: Tint to the color you want to see in the storyboard editor, this doesn't affect the icon color when your app is run.

Xcode Storyboard Editor


Step 3

Steps 1 & 2 will change the color for the selected icon. If you still want to change the color of the unselected items, you need to do it in code. I haven't found a way to do it via the storyboard editor.

Create a custom tab bar controller class...

//  TabBarController.swift

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // make unselected icons white
        self.tabBar.unselectedItemTintColor = UIColor.white
    }
}

... and assign the custom class to your tab bar scene controller.

Xcode Storyboard Editor


If you figure out how to change the unselected icon color via the storyboard editor please let me know. Thanks!

Derek Soike
  • 11,238
  • 3
  • 79
  • 74
  • `self.tabBar.unselectedItemTintColor = UIColor.white` `self.tabBar.tintColor = #colorLiteral(red: 0.2, green: 0.7333333333, blue: 0.3450980392, alpha: 1)` Work for me – Wahab Khan Jadon May 22 '20 at 06:07
14

This best way is to change Image Tint in storyboard

enter image description here

Andy Weinstein
  • 2,639
  • 3
  • 21
  • 32
Jadian
  • 4,144
  • 2
  • 13
  • 10
10

put this code in the viewDidLoad of the view controller that you want to change the color of

[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
Nick
  • 101
  • 6
8

You can also set selected image bar tint color by key path:

enter image description here

Hope this will help you!! Thanks

Arvind Kumar
  • 2,371
  • 1
  • 18
  • 25
7

XCode 8.2, iOS 10, Swift 3: now there's an unselectedItemTintColor attribute for tabBar:

self.tabBar.unselectedItemTintColor = UIColor(red: 0/255.0, green: 200/255.0, blue: 0/255.0, alpha: 1.0)
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
ComboPrime
  • 71
  • 1
  • 1
6

You can change colors UITabBarItem by storyboard but if you want to change colors by code it's very easy:

// Use this for change color of selected bar

   [[UITabBar appearance] setTintColor:[UIColor blueColor]];

// This for change unselected bar (iOS 10)

   [[UITabBar appearance] setUnselectedItemTintColor:[UIColor yellowColor]];

// And this line for change color of all tabbar

   [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
Genevios
  • 1,115
  • 1
  • 16
  • 29
5

Somehow we are not able to change the Tab Bar selected item Tint color using storyboard alone, hence I added below code in my ViewDidLoad, hope this helps.

[[UITabBar appearance] setTintColor:[UIColor whiteColor]]; 
skypirate
  • 663
  • 1
  • 7
  • 13
5

Add this code in your app delegate -did_finish_launching_with_options function

UITabBar.appearance().tintColor = UIColor( red: CGFloat(255/255.0), green: CGFloat(99/255.0), blue: CGFloat(95/255.0), alpha: CGFloat(1.0) )

put the RGB of the required color

SHINTO JOSEPH
  • 377
  • 4
  • 17
5

Image Tint from storyboard worked for me.

enter image description here

Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30
4

This is the solution in Swift 3 that works in iOS 10:

Firstly, you create your own tab bar controller subclass and add it to your tab controller in your storyboard. In the viewDidLoad() method you can then customize the tab bar. It should be stated here that the tintColor attribute of the tabBar represents the color of the selected item not the color of the unselected ones! In order to change the color of the unselected items, I recommend looping through each item and use the original colors of your images, so they are not rendered as grey automatically.

class CustomTabBarVC: UITabBarController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.tabBar.tintColor = AppColor.normalRed
        self.tabBar.barTintColor = .white
        self.tabBar.isTranslucent = true

        if let items = self.tabBar.items
        {
            for item in items
            {
                if let image = item.image
                {
                    item.image = image.withRenderingMode( .alwaysOriginal )
                }
            }
        }
    }
}

The only downside with this approach is that your item images must already have the desired color you aim for.

Andi
  • 8,154
  • 3
  • 30
  • 34
2

You can subclass the UITabBarController, and replace the one with it in the storyboard. In your viewDidLoad implementation of subclass call this:

[self.tabBar setTintColor:[UIColor greenColor]];
chancyWu
  • 14,073
  • 11
  • 62
  • 81
  • getting: `'UITabBar' does not have a member named 'setSelectedImageTintColor'` – Deekor Nov 10 '14 at 19:37
  • Also i am using swift so I put: `self.tabBar.setSelectedImageTintColor = UIColor.greenColor` not sure if that is correct or not – Deekor Nov 10 '14 at 19:39
  • 2
    @Deekor it's `tintColor` not `selectedImageTintColor`, btw `selectedImageTintColor` is deprecated in iOS 8. – chancyWu Nov 11 '14 at 01:58