Set the image and text color when TabBarItem is selected,

Source: Internet
Author: User

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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.