UITabbaritem quick colors

Hi i am trying to figure out how to use the colors i want for mine tabBar

.

I know how to change the background, I also know how to change the color and text tabbar.item

. but I cannot figure out how:

  • the default is gray for an item with an unselected tab
  • to change the color if an item is selected (and I'm using the render mode always the original reason, I can't find any other way to remove the default gray color from an unselected tab item).

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    
        tabBarItem.title = "test"
        tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)   
    
    }
    
          

how can i use the color i want in the state i want?

+3


source to share


2 answers


A UITabBar

has a property tintColor

, but this sets the hue for the selected image, not unselected. You are setting the unselected image correctly AFAIK. To change the color of the selected image you can use tintColor

on UITabBar

(if you want all images to have the same hue) or set UITabBarItem

selectedImage

with the rendering mode as AlwaysOriginal

.

tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)

      

I installed UIImage as an optional option because you probably want it to work if there is no image file. This will help ensure that your images are actually loading and not failing :-)



You can also set a color for the label, otherwise the text will not match your image colors. Below are the defaults for all UITabBarItem

, but you can set (or override) it for each position.

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal)

      

+8


source


This is how you do it in fast 3/4



  UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .selected)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .normal)

      

+3


source







All Articles