UITabBarController (Introduction and setup), uitabbarcontroller

Source: Internet
Author: User

UITabBarController (Introduction and setup), uitabbarcontroller




[Objc]View plaincopy
  1. -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
  2. {
  3. Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
  4. // Override point for customization after application launch.
  5. Self. window. backgroundColor = [UIColor whiteColor];
  6. # Pragma mark-set tabBarItem
  7. # Pragma mark first view ViewController
  8. HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];
  9. // Set the title text in the tab bar under view A (refer to or QQ)
  10. TabBarViewA. tabBarItem. title = @"";
  11. // Set the image in the label bar under view A (because you do not have any image, you can set A name here)
  12. // TabBarViewA. tabBarItem. image = [UIImage imageNamed: @ "1.png"];
  13. // Set the label bar information prompt in the ---- under view A (hold: badgeValue is NSString type, Set 3 as follows, just as there are 3 unaccepted QQ messages, give A reminder)
  14. TabBarViewA. tabBarItem. badgeValue = @ "3 ";
  15. // Ios7 discarded ---- one image is displayed when the tab is selected, and another image is displayed when the tab is not selected
  16. // [TabBarViewA. tabBarItem setFinishedSelectedImage: actionMenu. selectedIcon withFinishedUnselectedImage: actionMenu. icon];
  17. // Ios7 method (no image, so the image in the Code is a casual name, no practical significance)
  18. // TabBarViewA. tabBarItem. selectedImage = actionMenu. selectedIcon;
  19. # Pragma mark second view ViewController
  20. // ViewController of the second view
  21. HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];
  22. // Set the label bar under View B
  23. // Set it with the identifier provided by the system (which can be equivalent to icons and text) (parameter: UITabBarSystemItem is an enumeration value. You can find it in the API provided by the system if you want it)
  24. TabBarViewB. tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemSearch tag: 1];
  25. // Set the label bar information prompt under View B
  26. TabBarViewB. tabBarItem. badgeValue = @ "GO ";
  27. # Pragma mark third view ViewController
  28. HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];
  29. TabBarViewC. tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemDownloads tag: 2];
  30. // Set the label bar information prompt under View B
  31. TabBarViewC. tabBarItem. badgeValue = @ "new ";
  32. # Pragma mark fourth view ViewController
  33. HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];
  34. TabBarViewD. tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemFavorites tag: 3];
  35. // Set the label bar information prompt under View B
  36. TabBarViewD. tabBarItem. badgeValue = @ "99 ";
  37. # Pragma mark fifth view ViewController
  38. HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];
  39. TabBarViewE. tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemHistory tag: 4];
  40. // Set the label bar information prompt under View B
  41. TabBarViewE. tabBarItem. badgeValue = @ "sky ";
  42. # Pragma mark sixth view ViewController (the maximum number of views displayed by default is 5)
  43. /* If you have added more than five items to the viewControllers attribute, the tab bar controller will automatically insert a special view controller,
  44. Called More view controller, this controller manages More than items. This More view controller provides a custom interface,
  45. Displays redundant view controllers in table mode, and the number of view controllers is unlimited */
  46. HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];
  47. TabBarViewF. tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemContacts tag: 5];
  48. // Set the tag bar prompt in the F View
  49. TabBarViewF. tabBarItem. badgeValue = @ "AG ";
  50. # Pragma mark-set TabBarController
  51. // Create TabBarController
  52. UITabBarController * tabBarController = [[UITabBarController alloc] init];
  53. // TabBarController is placed at the bottom by default. If you want to adjust the position, perform the following two operations (44 is the standard height of TabBarController and UINavigationController on the iPhone)
  54. // CGRect frame = CGRectMake (0, 20,320, 44 );
  55. // TabBarController. tabBar. frame = frame;
  56. // Each tab must have a content view controller -------> viewControllers attribute, which is used to store the number of page switches in the TabBarController of an application.
  57. TabBarController. viewControllers = [NSArray arrayWithObjects: tabBarViewA, tabBarViewB, tabBarViewC, tabBarViewD, tabBarViewE, tabBarViewF, nil];
  58. // Set coloring
  59. TabBarController. tabBar. tintColor = [UIColor greenColor];
  60. // When setting the selected Image
  61. TabBarController. tabBar. selectedImageTintColor = [UIColor brownColor];
  62. // Set the background image (no image, no setting)
  63. // TabBarController. tabBar. backgroundImage = [UIImage imageNamed: @ "@"];
  64. // Set the default ViewController view when the program starts (set to 3, there are 5 viewcontrollers in total, and the view displayed when the program comes in is 4th-tabBarViewD, subscript starts from 0)
  65. TabBarController. selectedIndex = 3;
  66. Self. window. rootViewController = tabBarController;
  67. [Self. window makeKeyAndVisible];
  68. Return YES;
  69. }

@ The final result is as follows:





What is the difference between UITabBar and UITabBarController?

One is a control, the other is a view controller, and the View Controller contains a tabbar control! When you jump from view A to view B, If you want B to have tabbar effect, you can let B inherit from tabbarcontroller directly or use viewcontroller and add tabbar in it.
 
There are some issues with the UITabBarController developed by IOS.

Specify each uitabbaritem in the uitabbarcontroller assembly code, instead of in each included viewcontroller.

UITabbarController * tabController = [[UITabbarController alloc] init];
Self. vc1 = [[UIViewController alloc] init];
Slef. vc1.tabbarItem = ......;

Self. vc2 = [[UIViewController alloc] init];
Self. vc2.tabbarItem = ....;

......

NSArray * vcs = @ [self. vc1, self. vc2,...];

[TabController setViewControllers: vcs];

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.