[IOS development-28] Six methods for creating a UITabBarController label controller and custom UITabBarItem text images are described as well,

Source: Internet
Author: User

[IOS development-28] Six methods for creating a UITabBarController label controller and custom UITabBarItem text images are described as well,

1. A simple creation process (this method is not recommended for actual projects, but only for demonstration and understanding of principles)


In AppDelegate. m:

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// create a navigation controller UIViewController * vc1 = [[UIViewController alloc] init]; UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController: vc1]; nav1.title = @ "Homepage"; // create three view controllers UIViewController * vc2 = [[UIViewController alloc] init]; vc2.title = @ "favorites"; UIViewController * vc3 = [[UIViewController alloc] init]; vc3.title = @ "Search"; UIViewController * vc4 = [[UIViewController alloc] init]; vc4.title = @ "set"; // create an array to add the instances of the preceding navigation controller and three view controllers to NSArray * arr1 = [[NSArray alloc] initWithObjects: nav1, vc2, vc3, vc4, nil]; // instantiate a UITabBarController first, then assign the preceding array to its viewControllers attribute UITabBarController * tb1 = [[UITabBarController alloc] init]; tb1.viewControllers = arr1; // The most important thing is to assign the previously instantiated tag controller to self. in this way, the rootViewController of the window can be displayed, which is the same as that of the navigation controller. window. rootViewController = tb1; // Override point for customization after application launch. return YES ;}

Running effect:


Ii. Notes


(1) directly assign the array to the viewControllers attribute of the tag controller instance. Generally, this is not the case when you do the project. The above is just a demonstration of the entire implementation process of the principle.


(2) UITabBarController has two sub-views, one of which is the UITabBar part. When put in it, UITabBarItem is the one of the following four switches. The other is the content part, which is several views. Click different labels to display different views. Therefore, if we load the View Controller, we may modify the view. The custom tag is modified for UITabBarItem.


(3) Size: UITabBarItem has image icons and text, and the image size is 50*50. For specific parameters, click here.


Iii. Introduction to UITabBarItem attributes and six creation methods


We created six view controllers and imported their header files. Therefore, in the AppDelegate. m file:

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Method 1: call the image attribute ViewController * vc0 = [[ViewController alloc] init] through the tabBarItem of the navigation Controller; UINavigationController * nav0 = [[UINavigationController alloc] initWithRootViewController: vc0]; // use nav0.tabBarItem. title = @ "interface 1"; used to assign titles. the title value cannot be obtained. // you can use the following method to assign a title. tabBarItem. title value, but the following method directly sets navigationItem. the title is also assigned with a value. // The following method is used first: nav0.title = @ "interface 1"; // The value of nav0.tabBarItem is assigned to the image. image = [UIImage imageNamed: @ "Slice@1x.png"]; // Method 2: Use the setFinishedSelectedImage method on the tabBarItem of the View Controller, set the status of the selected unselected image. // However, this method does not seem to be recommended and cannot be used. It has been abandoned in iOS7. // we recommend that you use initWithTitle: image: selectedImage: this initialization method ViewController1 * vc1 = [[ViewController1 alloc] init]; // vc1.title = @ "interface 2"; // [vc1.tabBarItem setFinishedSelectedImage: [UIImage imageNamed: @ "1@1x.png"] identifier: [UIImage imageNamed: @ "1-selected@1x.png"]; UITabBarItem * tb0 = [[UITabBarItem alloc] initWithTitle: @ "interface 2" image: [UIImage imageNamed: @ "Slice@1x.png"] selectedImage: [UIImage imageNamed: @ "Slice-selected@1x.png"]; vc1.tabBarItem = tb0; // method 3: use the tabBarItem of the View Controller to call the image attribute ViewController2 * vc2 = [[ViewController2 alloc] init]; vc2.title = @ "Interface 3"; vc2.tabBarItem. image = [UIImage imageNamed: @ "Slice@1x.png"]; // Method 4: Set the logo, where badgeValue is an attribute of tabBarItem, the value is the string ViewController3 * vc3 = [[ViewController3 alloc] init]; vc3.title = @ "interface 4"; vc3.tabBarItem. image = [UIImage imageNamed: @ "Slice@1x.png"]; vc3.tabBarItem. badgeValue = @ "6"; // Method 5: first set a UITabBarItem that comes with the system, and then assign it to the tabBarItem of the View Controller. // after this method is created, set again. title or. tabBarItem. viewController4 * vc4 = [[ViewController4 alloc] init]; UITabBarItem * tb1 = [[UITabBarItem alloc] initWithTabBarSystemItem: Invalid tag: 0]; vc4.tabBarItem = tb1; // Method 6: initialize a UITabBarItem with the title and image, and assign it to the tabBarItem of the View Controller. title and. tabBarItem. set the image. The effect is similar. // ViewController5 * vc5 = [[ViewController5 alloc] init]; UITabBarItem * tb2 = [[UITabBarItem alloc] initWithTitle: @ "interface 6" image: [UIImage imageNamed: @ "Slice@1x.png"] tag: 1]; vc5.tabBarItem = tb2; // The following is the total number of the above, initialize a UITabBarController and assign the array value to the viewControllers attribute of UITabBarController. // do not forget to modify the NSArray * arr2 = [[NSArray alloc] initWithObjects: nav0, vc4, vc1, vc3, vc2, vc5, nil]; UITabBarController * tbCon1 = [[UITabBarController alloc] init]; tbCon1.viewControllers = arr2; self. window. rootViewController = tbCon1; // Override point for customization after application launch. return YES ;}

(1) only five tabbar Icons can be displayed. If more than five icons are displayed, all the icons are placed in more.


(2) There are two main methods: one is to directly set text and images by using the. title and. tabBarItem. image attributes of the existing navigation controller or view controller. Another way is to create a UITabBarItem (the image is not selected in the text) and assign this UITabBarItem to the. tabBarItem attribute of the navigation controller or view controller.


(3) The second method is recommended, because it can define different states such as text, selected image, and unselected image at one time when creating UITabBarItem.


4. More information on tabbar


(1) When there are More than five tags, the tag controller will automatically create a moreNavigationController, that is, click More, which is a navigation controller.


(2) After clicking More, we can click Edit and drag the corresponding icons to rearrange their display order.


V. Questions about English and Chinese languages


The above "More" and "Edit" are both in English. How can they be displayed in Chinese? Localization is required. By default, this program only supports English. Therefore, no matter what language your mobile phone is set to, the text that comes with the system will be in English. We need to set the program to two languages (Chinese and English), so that the program will display the corresponding language based on the language settings of the user's mobile phone.


Method: Add Localizations to Info. plist and add the option "simplified Chinese" to Localizations (only English is used by default ).



In this case, close the job! (If your text is still in English, click CMD + SHIFT + H to return to the main interface and set the simulator language to simplified Chinese in Setting)




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.