Set the image and text color when TabBarItem is selected,
TabBarController is a Controller that is frequently used during ios development, but it often encounters some problems during use, how to modify the color of text and image when TabBar is selected.
If necessary, you can download the complete code from here.
First, create a new project, use TabBarController, and set the default image and the selected image for TabBarItem in the Child Controller of TabBarController, as shown in:
Storyboard:
ValueMeaningUIImageRenderingModeAutomaticAutomatically adjust the rendering mode based on the position of the image (default)UIImageRenderingModeAlwaysOriginalThe original status of the image is always drawn. tint color is not applicable.UIImageRenderingModeAlwaysTemplateUse tint color to draw an image and ignore the color information of the image.
To solve the problem, you only need to set the render mode of the selected image.
Open one of the view controllers and add the following code in the viewDidLoad method:
1 UIImage * image = [self. tabBarItem. selectedImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; 2 self. tabBarItem. selectedImage = image;View Code
Run the program again. You can see that the selected tab bar item has changed to a red image, but the color of the text has changed ~
We can use text attribute to set the text style and add the following content in the above Code:
1 [self. tabBarItem setTitleTextAttributes: @ {NSForegroundColorAttributeName: [UIColor redColor]} forState: UIControlStateSelected];View Code
Compile and run the program. We can see that all the words and images are what we want ~