A.需求
1.类似QQ、微信顶部或者底部的窗口转换导航条
2.给每个页面添加相应内容
B.UITabBarController
1.基本概念:
(1)内容高度
iOS7之前内容高度为:屏幕高度 - 状态栏高度44 - 底部导航条高度49
iOS7及之后,内容高度为整个屏幕
2.使用代码创建初始化
(1)创建一个UITabBarController
UITabBarController的使用步骤
- 初始化UITabBarController
- 设置UIWindow的rootViewController为UITabBarController
- 根据具体情况,通过addChildViewController方法添加对应个数的子控制器
(2)添加子控制器
UITabBarController添加控制器的方式有2种
- 添加单个子控制器
- (void)addChildViewController:(UIViewController *)childController;
- 设置子控制器数组
@property(nonatomic,copy) NSArray *viewControllers;
如果UITabBarController有N个子控制器,那么UITabBar内部就会有N个UITabBarButton作为子控件
如果UITabBarController有4个子控制器,那么UITabBar的结构大致如下图所示
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // 设置window
self.window = [[UIWindow alloc] init];
self.window.frame = [[UIScreen mainScreen] bounds];
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible]; // 设置一个UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController; // 添加子控制器
UIViewController *c1 = [[UIViewController alloc] init];
c1.view.backgroundColor = [UIColor redColor];
// [tabBarController addChildViewController:c1]; UIViewController *c2 = [[UIViewController alloc] init];
c2.view.backgroundColor = [UIColor blueColor];
// [tabBarController addChildViewController:c2]; UIViewController *c3 = [[UIViewController alloc] init];
c3.view.backgroundColor = [UIColor greenColor]; tabBarController.viewControllers = @[c1,c2,c3]; return YES;
}
(3)设置导航条选项样式
UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定
UITabBarItem有以下属性影响着UITabBarButton的内容
标题文字
@property(nonatomic,copy) NSString *title;
标题文字
@property(nonatomic,copy) NSString *title;
图标
@property(nonatomic,retain) UIImage *image;
选中时的图标
@property(nonatomic,retain) UIImage *selectedImage;
提醒数字
@property(nonatomic,copy) NSString *badgeValue;
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // 设置window
self.window = [[UIWindow alloc] init];
self.window.frame = [[UIScreen mainScreen] bounds];
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible]; // 设置一个UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController; // 添加子控制器
UIViewController *c1 = [[UIViewController alloc] init];
c1.view.backgroundColor = [UIColor redColor];
c1.tabBarItem.title = @"红色";
// [tabBarController addChildViewController:c1]; UIViewController *c2 = [[UIViewController alloc] init];
c2.view.backgroundColor = [UIColor blueColor];
c2.tabBarItem.title = @"蓝色";
// [tabBarController addChildViewController:c2]; UIViewController *c3 = [[UIViewController alloc] init];
c3.view.backgroundColor = [UIColor greenColor];
c3.tabBarItem.title = @"绿色"; tabBarController.viewControllers = @[c1,c2,c3]; return YES;
}
3.使用storyboard创建初始化
(1)拖入UITabBarController
(2)拖入N个UIViewController待用
(3)按照顺序连线,设置UITabBarController的子控制器
(4)设置子控制器的TabBarItem
out:
注意:Selected Image不设置,系统就会自动将原图编程蓝色作为 Selected Image
4.子控制器的生命周期
(1)单个子控制器的生命周期和普通的控制器生命周期一样
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@ - viewDidLoad", self.class);
} - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%@ - viewWillAppear", self.class);
} - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%@ - viewDidAppear", self.class);
} - (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"%@ - viewWillDisappear", self.class);
} - (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"%@ - viewDidDisappear", self.class);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"%@ - didReceiveMemoryWarning", self.class);
} - (void)viewWillUnload {
[super viewWillUnload];
NSLog(@"%@ - viewWillUnload", self.class);
} - (void)viewDidUnload {
[super viewDidUnload];
NSLog(@"%@ - viewDidUnload", self.class);
}
(2)两个子控制器切换的生命周期
a.出现第一个子控制器view
2014-12-27 21:11:44.338 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidLoad
2014-12-27 21:11:44.340 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewWillAppear
2014-12-27 21:11:44.340 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewWillAppear
2014-12-27 21:11:44.407 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidAppear
b.切换到第二个控制器view
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewDidLoad
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewWillAppear
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewWillDisappear
2014-12-27 21:12:10.054 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidDisappear
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewWillAppear
2014-12-27 21:12:10.042 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewWillDisappear
2014-12-27 21:12:10.054 UITabBarControllerByStoryboard[22567:590996] OneViewController - viewDidDisappear
2014-12-27 21:12:10.055 UITabBarControllerByStoryboard[22567:590996] TwoViewController - viewDidAppear
C.App框架
在每个子控制器之前再包装上一个NavigationController