#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" @implementation RootViewController -(void)viewWillAppear:(BOOL)animated { UITabBarItem* item = self.tabBarController.tabBar.items[0]; item.badgeValue = @"12"; } @end
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { RootViewController* r1= [[RootViewController alloc] init]; UITabBarItem* item1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0]; r1.tabBarItem = item1; r1.title = @"第一个"; r1.tabBarItem.title =@"第一个"; r1.tabBarController.title=@"第一个"; r1.view.backgroundColor = [UIColor blueColor]; RootViewController* r2= [[RootViewController alloc] init]; r2.title = @"第二个"; r2.tabBarItem.image = [UIImage imageNamed:@"3.png"]; r2.view.backgroundColor = [UIColor redColor]; RootViewController* r3= [[RootViewController alloc] init]; UITabBarItem* item3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2]; r3.tabBarItem = item3; r3.view.backgroundColor = [UIColor greenColor]; RootViewController* r4= [[RootViewController alloc] init]; UITabBarItem* item4 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:3]; r4.tabBarItem = item4; r4.view.backgroundColor = [UIColor grayColor]; RootViewController* r5= [[RootViewController alloc] init]; UITabBarItem* item5 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:4]; r5.tabBarItem = item5; r5.view.backgroundColor = [UIColor darkGrayColor]; RootViewController* r6= [[RootViewController alloc] init]; UITabBarItem* item6 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:5]; r6.tabBarItem = item6; r6.view.backgroundColor = [UIColor darkGrayColor]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UITabBarController* t = [[UITabBarController alloc] init]; t.tabBar.backgroundColor = [UIColor brownColor]; t.viewControllers = @[r1,r2,r3,r4,r5,r6]; self.window.rootViewController = t; [self.window makeKeyAndVisible]; return YES; }
个人对这个ui组件使用的理解
1,虽然这个UITabBarController继承于UIViewController,但UIViewController也保留了访问其属性,这样方便了通讯。
2,即使继承于UINavigationController的话,也能访问其TabBarController属性,就是多了导航属性,如多了导航条。
3,UITabBarController是数组管理,每个元素是个ViewController,同时又有tabBar属性,每个元素是tabBarItem,这个可以替换。
4,tabBar最多显示5个元素,大于5个会显示更多按钮。