Set the color of the tabBar image, height, and title,

Source: Internet
Author: User

Set the color of the tabBar image, height, and title,

Implemented the following content:

1. Set the selected and unselected tabBarItem images. The images are full of items;

2. The height of tabBar is adjusted;

3. changed the title Color and position.

 

------------ The code is as follows:

 

--- TabBarC. m ---

# Import "TabBarC. h"

# Import "ViewController. h"

# Import "SecViewController. h"

 

# Define CustomTabBarHeight 60 // you do not need to change the height to self. tabBar. frame. size. height.

# Define CustomTabBarWidth self. tabBar. frame. size. width

 

@ Interface TabBarC ()

 

@ End

 

@ Implementation TabBarC

 

-(Void) viewDidLoad {

[Super viewDidLoad];

// Initialize the sub-Controller

ViewController * onevc = [[ViewController alloc] init];

SecViewController * twovc = [[SecViewController alloc] init];

// Place the VC, unselected Image Library, and the selected image into the array.

NSArray * onevcArr = @ [onevc, @ "one_icon", @ "sel_one_icon"];

NSArray * twovcArr = @ [twovc, @ "two_icon", @ "sel_two_icon"];

NSArray * vcArr = @ [onevcArr, twovcArr];

[Self addToTabBar: vcArr];

}

 

// Add the sub-controller to the tabBar

-(Void) addToTabBar :( NSArray *) array {

For (NSInteger I = 0; I <array. count; I ++ ){

NSArray * everyVCArr = array [I];

UIViewController * everyVC = everyVCArr [0];

EveryVC. title = @ "vc title ";

EveryVC. view. backgroundColor = [UIColor whiteColor];

// Move the inserted image 5.5 down. When the top and bottom are not the opposite, the image changes.

EveryVC. tabBarItem. imageInsets = UIEdgeInsetsMake (5.5, 0,-5.5, 0 );

// Width of tabBarItem

NSInteger itemWidth = CustomTabBarWidth/array. count;

# Pragma ------- Note: (if tabBarItem. image is not set, other tabBarItem settings are invalid)

# Pragma ------- no suitable images of different sizes are prepared here. To show the effect, scale the image to the appropriate size. The scaling steps can be omitted.

// -------- Set the image when it is not selected, and set the image to the original state

EveryVC. tabBarItem. image = [[self editImage: everyVCArr [1] toSize: CGSizeMake (itemWidth, CustomTabBarHeight)] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

// Set the selected image. The image is set to the original state.

EveryVC. tabBarItem. selectedImage = [[self editImage: everyVCArr [2] toSize: CGSizeMake (itemWidth, CustomTabBarHeight)] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

// Adjust the title Position of tabBarItem

[EveryVC. tabBarItem setTitlePositionAdjustment: UIOffsetMake (0, 5)];

// Set the font color

NSMutableDictionary * textAttribute = [NSMutableDictionary dictionary];

TextAttribute [NSForegroundColorAttributeName] = [UIColor blackColor];

[EveryVC. tabBarItem setTitleTextAttributes: textAttribute forState: UIControlStateNormal];

// Set the font color

[EveryVC. tabBarItem setTitleTextAttributes: @ {NSForegroundColorAttributeName: [UIColor redColor]} forState: UIControlStateSelected];

EveryVC. tabBarItem. title = @ "";

UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController: everyVC];

[Self addChildViewController: nvc];

}

NSLog (@"

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.