How to change UITabBar Selection color

前端 未结 13 804
遇见更好的自我
遇见更好的自我 2020-12-13 09:02

I need to change the selection color of UITabBar from default blue to red. How do we do this.

相关标签:
13条回答
  • 2020-12-13 09:28

    Update September 2017: It's been two years since I've written this answer and since it's receiving upvotes regularly, I should say this is probably the worst possible answer to this question, it's error prone, likely to break because of iOS updates, hard to debug, etc., so please don't do the things I've written and apply better solutions such as subclassing UITabBar or UITabBarController. Thanks.

    You can do this by setting a "tintColor" attribute (Key Path) for you UITabBar.

    1. Select the UITabBar in the document outline. (NOT the Controller with the yellow icon.)
    2. Select Identity Inspector in the Utilities area.
    3. Click the + in "User Defined Runtime Attributes."
    4. Add a "tintColor" Key Path of type "Color" and the color you want.

    This should do it. You can check it against the screenshot below.

    More on this: There's a "Tint" attribute in Identity Inspector of UITabBar which I believed would do the exact same thing but apparently, it does nothing. It's default value is the exact default fill color when a UITabBarItem is selected, so my guess is it would be fixed in the stable release Xcode 7. Fingers crossed.

    0 讨论(0)
  • 2020-12-13 09:28

    Simply change the following property in Interface Builder for the TabBar

    Obviously in my case its White.

    0 讨论(0)
  • 2020-12-13 09:30

    To achieve above result perform following steps.

    Step 1: Add your desired images in Assets.xcassets, and make sure they Render As: Default

    Step 2: Select your UITabBar object and set Image Tint color, this color will be selected tab color

    Step 3: Select UITabBar object and add Key Path: unselectedItemTintColor, Type: Color, Value: Choose color for unselected item in User Defined Runtime Attributes.

    All done.

    0 讨论(0)
  • 2020-12-13 09:32

    In IOS5, UITabBar has a selectedImageTintColor property which does what you need.

    0 讨论(0)
  • 2020-12-13 09:35

    I've been searching for a way to set the selected text color of a UITabBarItem and have found a dead simple method, using the UIAppearance protocol.

    [UITabBarItem.appearance setTitleTextAttributes:@{
            UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
    
    [UITabBarItem.appearance setTitleTextAttributes:@{
            UITextAttributeTextColor : [UIColor purpleColor] }     forState:UIControlStateSelected];
    

    Please excuse the awful colors!

    0 讨论(0)
  • 2020-12-13 09:35

    Swift 5 Programatically

    It is pretty easy in Swift 5.

    In your TabBarController write this:

    tintColor = UIColor.red  
    

    That's it

    0 讨论(0)
提交回复
热议问题