三级控制器
- UITabBarController
- UINavigationController
- 一个导航控制器对应一个视图控制器
- 标签控制器是管理几个固定的控制器,子控制器是并列的,每一个分栏控制器只有一个UITabBar视图,用于显示UITabBatItem实例,UITabBarItem由当前的试图控制器来管理,这一点与导航控制器中的UIBarButtonitem相同。
- UITabBarController用数组来管理视图控制器,而导航控制器所管理的试图控制器是上下层级的关系

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
// 权限最高的给跟视图控制器,即标签控制器来控制导航控制器,导航控制器来控制视图控制器
self.window.rootViewController = rootVC;
return YES;
}```
###root h
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}
import <UIKit/UIKit.h>
@interface RootViewController : UITabBarController
@property(nonatomic, strong) UIView * tabBarView;
- (void)showTabBar:(BOOL)show;
@end```
root m
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #31595d}p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; min-height: 21.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s10 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s11 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s12 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s13 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s14 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}
#import "RootViewController.h"
// 引入子控制器
#import "ProfieViewController.h"
#import "ColaViewController.h"
#import "MessageViewController.h"
#import "UserViewController.h"
#import "MoreViewController.h"
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
CGFloat const tabBarHeight = 49;
CGFloat const buttonWidth = 64;
CGFloat const buttonHeight = 45;
@interface RootViewController ()
@property (nonatomic, strong) UIImageView *selectImageView;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 隐藏系统默认样式
self.tabBar.hidden = YES;
[self initViewController];
[self initTabBarView];
}
#pragma mark - 初始化控制器
- (void)initViewController{
ProfieViewController *p = [[ProfieViewController alloc] init];
ColaViewController *c = [[ColaViewController alloc] init];
UserViewController *u = [[UserViewController alloc] init];
MessageViewController *m = [[MessageViewController alloc] init];
MoreViewController *more = [[MoreViewController alloc] init];
NSArray *vcArray = @[p,c,u,m,more];
NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];
// for循环创建导航控制器
for (int i = 0; i < vcArray.count; i++) {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vcArray[i]];
[tabArray addObject:nav];
}
// 将导航控制器给标签控制器
self.viewControllers = tabArray;
}
#pragma mark - 自定义标签栏
- (void)initTabBarView{
// 初始化标签工具栏视图
self.tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight - tabBarHeight, screenWidth, tabBarHeight)];
// 设置TabBarView的背景颜色 用图片背景来设置
_tabBarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mask_navbar"]];
// 创建一个数组 用来放置TabBarView图片按钮
[self.view addSubview:_tabBarView];
// 新语法创建数组
NSArray *imageArray = @[@"home_tab_icon_1",@"home_tab_icon_2",@"home_tab_icon_3",@"home_tab_icon_4",@"home_tab_icon_5"];
for (int i = 0; i < imageArray.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
// button frame的设置 这里注意一下 (tabBarHeight - buttonHeight)/2 的作用是为了让Button居中 要牢记这个方法
button.frame = CGRectMake(buttonWidth * i, (tabBarHeight - buttonHeight) / 2, buttonWidth, buttonHeight);
[button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
// 设置tag 值 记得tag值要设置100以后的 因为在iOS中100以前的数值是有别的用处的额
button.tag = 100 + i;
[self.tabBarView addSubview:button];
}
// 初始化选中图片的视图
_selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
_selectImageView.image = [UIImage imageNamed:@"home_bottom_tab_arrow"];
[_tabBarView addSubview:_selectImageView];
}
- (void)btnAction:(UIButton *)sender{
// 根据tag值判断当前的索引并赋值给当前的TabBarController
self.selectedIndex = sender.tag - 100;
// 添加滑动动画
[UIView animateWithDuration:0.2 animations:^{
// 中心点重合
_selectImageView.center = sender.center;
}];
}
- (void)showTabBar:(BOOL)show{
CGRect frame = self.tabBarView.frame;
if (show) {
frame.origin.x = 0;
}else{
frame.origin.x = -screenWidth;
}
[UIView animateWithDuration:0.2 animations:^{
self.tabBarView.frame = frame;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end```
###首页 m
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #31595d}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; min-height: 21.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s6 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s10 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s11 {font-variant-ligatures: no-common-ligatures; color: #31595d}
import "ProfieViewController.h"
import "ModelViewController.h"
import "RootViewController.h"
CGFloat const writeBtnWidth = 33;
CGFloat const writeBtnHeight = 32;
@interface ProfieViewController ()
@end
@implementation ProfieViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.self.title = @"首页";
self.view.backgroundColor = [UIColor redColor];[self initNavButton];
[self initPushButton];
}
pragma mark - 自定义导航栏
-
(void)initNavButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, writeBtnWidth, writeBtnHeight);
[button setBackgroundImage:[UIImage imageNamed:@"write"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(presentAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBBI = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = btnBBI;
} -
(void)presentAction{
NSLog(@"首页左上角按钮");ModelViewController *m = [[ModelViewController alloc] init];
[self presentViewController:m animated:YES completion:nil];
} -
(void)initPushButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 200, 40);[button setTitle:@"push" forState:UIControlStateNormal];
// 此方法和setTitle两者只能二选一
// [button setImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]
[button addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
} -
(void)pushAction{
NSLog(@"fdsalf");
ModelViewController *model = [[ModelViewController alloc] init];
// [self.navigationController pushViewController:model animated:YES];
// iOS8新方法 和Push一样的效果
[self.navigationController showViewController:model sender:nil];
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:NO];
}
pragma mark - 视图将要出现的时候出现标签栏
- (void)viewWillAppear:(BOOL)animated{
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end```
效果;
