1,每个视图控制器对应一个tabBarItem属性,每个视图控制器可以个性化定制自己的tabBarItem
[tabController setViewControllers :controllers];
self . window . rootViewController = tabController;
[ self . window makeKeyAndVisible ];
return YES ;
}
2,各个tabBarItem实例显示在UITabBar实例上。
3,点击不同的tabBarItem,能切换不同的视图控制器显示
4,UITabBar上最多可以显示5个TabBarItem,当向UITabBarController中添加超过5个viewController
的时候,第五个TabBarItem自动变成一个MORE按钮
5,当点击给你更多的时候,会切换到一个导航控制器,此导航控制器为UITabBarController自动生成
的moreNavigationController,里边放有其他未显示的viewController
6,同时右上角会有一个编辑按钮,点击编辑按钮,可以对所有视图控制器的位置进行变更
创建
XSViewController1.h到
XSViewController6.h 六个UIViewController类
<
XSAppDelegate.m>
#import
"XSAppDelegate.h"
@implementation XSAppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions
{
self . window = [[ UIWindow alloc ] initWithFrame :[[ UIScreen mainScreen ] bounds ]];
// Override point for customization after application launch.
self . window . backgroundColor = [ UIColor whiteColor ];
XSViewController1 *VC1 = [[ XSViewController1 alloc ] init ];
UINavigationController *navController = [[ UINavigationController alloc ] initWithRootViewController :VC1];
// // 这样赋值后,使用 .title 属性取不到值
// [navController.tabBarController setTitle:@" 界面 1"];
// 这样赋值后,当当与对 tabBarController.title 和 navigationItem.title 同时进行赋值
// 使用 tabBarItem.title 取值,能取到
[navController setTitle : @" 界面 1" ];
// 进行图片赋值
[navController. tabBarItem setImage :[ UIImage imageNamed : @"tabbar_account.png" ]];
XSViewController2 *VC2 = [[ XSViewController2 alloc ] init ];
[VC2 setTitle : @" 界面 2" ];
// 设置按钮选中和没选中的图片, iOS7 不能用这个方法
// [VC2.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabbar_appfree_press.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tabbar_appfree.png"]];
XSViewController3 *VC3 = [[ XSViewController3 alloc ] init ];
[VC3 setTitle : @" 界面 3" ];
// 只
[VC3. tabBarItem setImage :[ UIImage imageNamed : @"tabbar_limitfree.png" ]];
XSViewController4 *VC4 = [[ XSViewController4 alloc ] init ];
[VC3 setTitle : @" 界面 4" ];
VC4. tabBarItem . image = [ UIImage imageNamed : @"tabbar_limitfree.png" ];
// 设置微标
VC4. tabBarItem . badgeValue = @"10" ;
XSViewController5 *VC5 = [[ XSViewController5 alloc ] init ];
// 创建 UITabItem 的实例
UITabBarItem *item1 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemHistory tag :0];
VC5. tabBarItem = item1;
// 通过系统样式的初始化方式,在设置 title 。 image 属相,设置无效
VC5. title = @" 界面 5" ;
VC5. tabBarItem . image = [ UIImage imageNamed : @"tabbar_limitfree.png" ];
XSViewController6 *VC6 = [[ XSViewController6 alloc ] init ];
@implementation XSAppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions
{
self . window = [[ UIWindow alloc ] initWithFrame :[[ UIScreen mainScreen ] bounds ]];
// Override point for customization after application launch.
self . window . backgroundColor = [ UIColor whiteColor ];
XSViewController1 *VC1 = [[ XSViewController1 alloc ] init ];
UINavigationController *navController = [[ UINavigationController alloc ] initWithRootViewController :VC1];
// // 这样赋值后,使用 .title 属性取不到值
// [navController.tabBarController setTitle:@" 界面 1"];
// 这样赋值后,当当与对 tabBarController.title 和 navigationItem.title 同时进行赋值
// 使用 tabBarItem.title 取值,能取到
[navController setTitle : @" 界面 1" ];
// 进行图片赋值
[navController. tabBarItem setImage :[ UIImage imageNamed : @"tabbar_account.png" ]];
XSViewController2 *VC2 = [[ XSViewController2 alloc ] init ];
[VC2 setTitle : @" 界面 2" ];
// 设置按钮选中和没选中的图片, iOS7 不能用这个方法
// [VC2.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabbar_appfree_press.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tabbar_appfree.png"]];
XSViewController3 *VC3 = [[ XSViewController3 alloc ] init ];
[VC3 setTitle : @" 界面 3" ];
// 只
[VC3. tabBarItem setImage :[ UIImage imageNamed : @"tabbar_limitfree.png" ]];
XSViewController4 *VC4 = [[ XSViewController4 alloc ] init ];
[VC3 setTitle : @" 界面 4" ];
VC4. tabBarItem . image = [ UIImage imageNamed : @"tabbar_limitfree.png" ];
// 设置微标
VC4. tabBarItem . badgeValue = @"10" ;
XSViewController5 *VC5 = [[ XSViewController5 alloc ] init ];
// 创建 UITabItem 的实例
UITabBarItem *item1 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemHistory tag :0];
VC5. tabBarItem = item1;
// 通过系统样式的初始化方式,在设置 title 。 image 属相,设置无效
VC5. title = @" 界面 5" ;
VC5. tabBarItem . image = [ UIImage imageNamed : @"tabbar_limitfree.png" ];
XSViewController6 *VC6 = [[ XSViewController6 alloc ] init ];
//
确保
title
值可以被取到
VC6. title = @" 界面 6" ;
// 通过这种方式进行初始化,效果等同于直接为 VC6.tabBarItem.title VC6.tabBarItem.image 赋值
UITabBarItem *item2 = [[ UITabBarItem alloc ] initWithTitle : @" 界面 6" image :[ UIImage imageNamed : @"tabbar_limitfree.png" ] tag:1];
VC6. tabBarItem = item2;
NSArray *controllers = [ NSArray arrayWithObjects :navController,VC2,VC3,VC4,VC5,VC6, nil ];
UITabBarController *tabController = [[ UITabBarController alloc ] init ];
VC6. title = @" 界面 6" ;
// 通过这种方式进行初始化,效果等同于直接为 VC6.tabBarItem.title VC6.tabBarItem.image 赋值
UITabBarItem *item2 = [[ UITabBarItem alloc ] initWithTitle : @" 界面 6" image :[ UIImage imageNamed : @"tabbar_limitfree.png" ] tag:1];
VC6. tabBarItem = item2;
NSArray *controllers = [ NSArray arrayWithObjects :navController,VC2,VC3,VC4,VC5,VC6, nil ];
UITabBarController *tabController = [[ UITabBarController alloc ] init ];
[tabController setViewControllers :controllers];
self . window . rootViewController = tabController;
[ self . window makeKeyAndVisible ];
return YES ;
}