一、简介
UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ、微信、微博等应?。
二、UITabBarController的使用
1、首先初始化UITabBarController
2、设置UIWindow的rootViewController为UITabBarController
3、创建相应的子控制器(viewcontroller)
4、把子控制器添加到UITabBarController
代码:
// // AppDelegate.m // 微信 // // Created by Oran Wu on 15-11-5. // Copyright (c) 2015年 Xinxin. All rights reserved. // #import "AppDelegate.h" #import "WeChatViewController.h" #import "AddressBookViewController.h" #import "FindViewController.h" #import "MyViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //创建Window self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //创建子控制器 WeChatViewController *WeChatVC = [[WeChatViewController alloc] init]; AddressBookViewController *AddressBookVC = [[AddressBookViewController alloc] init]; FindViewController *FindVC = [[FindViewController alloc] init]; MyViewController *MyVC = [[MyViewController alloc] init]; //设置UITabBarButton的图片(normal)与选中时图片(selected) UIImage *WeChatImage = [UIImage imageNamed:@"tabbar_mainframe"]; UIImage *WeChatSeletedImage = [UIImage imageNamed:@"tabbar_mainframeHL"]; WeChatVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"微信" image:WeChatImage selectedImage:WeChatSeletedImage]; UIImage *AddressImage = [UIImage imageNamed:@"tabbar_contacts"]; UIImage *AddressSeletedImage = [UIImage imageNamed:@"tabbar_contactsHL"]; AddressBookVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:AddressImage selectedImage:AddressSeletedImage]; UIImage *FineImage = [UIImage imageNamed:@"tabbar_discover"]; UIImage *FineSeletedImage = [UIImage imageNamed:@"tabbar_discoverHL"]; FindVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:FineImage selectedImage:FineSeletedImage]; //提醒数字 FindVC.tabBarItem.badgeValue = @"1"; UIImage *MyImage = [UIImage imageNamed:@"tabbar_me"]; UIImage *MySeletedImage = [UIImage imageNamed:@"tabbar_meHL"]; MyVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:MyImage selectedImage:MySeletedImage]; //初始化各ViewController的导航控制器 UINavigationController *WeChatNav = [[UINavigationController alloc] initWithRootViewController:WeChatVC]; UINavigationController *AddressBookNav = [[UINavigationController alloc] initWithRootViewController:AddressBookVC]; UINavigationController *FindNav = [[UINavigationController alloc] initWithRootViewController:FindVC]; UINavigationController *MyNav = [[UINavigationController alloc] initWithRootViewController:MyVC]; //初始化一个控制器 UITabBarController *TabBarCtrl = [[UITabBarController alloc] init]; TabBarCtrl.viewControllers = [NSArray arrayWithObjects:WeChatNav,AddressBookNav,FindNav,MyNav, nil]; //设置控制器为Window的根控制器 self.window.rootViewController = TabBarCtrl; //设置window的背景颜色 self.window.backgroundColor = [UIColor whiteColor]; //设置Window为主窗口并显示出来 [self.window makeKeyAndVisible]; // Override point for customization after application launch. return YES; }
三、UITabBar
下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。
注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49。
在上面的程序中,UITabBarController有4个子控制器,所以UITabBar中有4个UITabBarButton,UITabBar的结构?大致如下图所示:
时间: 2024-10-12 22:14:05