Change UITabBarItem Un-selected Color Tint - Swift

Simply put, I would like to be able to change the coloring of not selected elements in my tab bar.

See the โ€œMost Viewedโ€ default subject barley below.

Here is the code I tried to implement:

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIColor.greenColor(), forKey: NSFontAttributeName), forState: UIControlState.Normal) 

enter image description here

However, using this code does not work. Does anyone know how to achieve this effect especially quickly?


+6
source share
2 answers

In UITabBarItem class docs:

By default, the actual unselected and selected images are automatically created from the alpha values โ€‹โ€‹in the original images. To prevent system staining, provide images using UIImageRenderingModeAlwaysOriginal.

The tooltip does not use UIImageRenderingModeAlwaysOriginal , it is important when to use it.

To prevent gray color for unselected items, you just need to prevent the system from coloring for the unselected image. Here's how to do it:

 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, any) of the image ONLY for the UNSELECTED state and leave the image as it is for the SELECTED state.

In addition, you may need to add a hue color for the tab bar to apply a different color for the SELECTED state (instead of the default iOS blue color). As shown in the screenshot above, you apply white color to the selected state:

 self.tabBar.tintColor = UIColor.whiteColor() 
+2
source

This seems to be just a syntax error; try like this:

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.greenColor()], forState: .Normal) 

or (to include an image, if not indicated above):

 UITabBarItem.appearance().setTintColor(UIColor.greenColor()); 
0
source

Source: https://habr.com/ru/post/975234/


All Articles