1.标签栏的建立
需要是建立 rootViewController类
//初始化视图控制器
- (void) initViewController
{
ProfileViewController *profileVC = [[ProfileViewController alloc]init];
MessageViewController *messageVC = [[MessageViewController alloc]init];
ColaViewController *colaVC = [[ColaViewController alloc]init];
UserViewController *userVC = [[UserViewController alloc]init];
MoreViewController *moreVC = [[MoreViewController alloc]init];
//新语法
NSArray *vcArray = @[profileVC,messageVC,colaVC,userVC,moreVC];
NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];
//初始化导航控制器
for (int i=0; i<vcArray.count; i++) {
UINavigationController *naCtrl = [[UINavigationController alloc]initWithRootViewController:vcArray[i]];
[tabArray addObject:naCtrl];
}
//将导航控制器赋值给标签控制器
self.viewControllers = tabArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//初始化标签工具栏
- (void) initTabBArView
{
//初始化标签工具栏视图
_tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - tabViewHeight, kScreenWith, kScreenHeight)];
_tabBarView.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"mask_navbar"]];
[self.view addSubview:_tabBarView];
//新语法创建数组,拿到图片
NSArray *imgArray = @[@"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 < imgArray.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:imgArray[i]] forState:UIControlStateNormal];
btn.frame = CGRectMake(btnWith * i, (tabViewHeight - btnWith)/2 , btnWith, btnHeight);
btn.tag = 100 + i ;
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarView addSubview:btn];
}
//初始化选中图片视图
_selectView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btnWith, btnHeight)];
_selectView.image = [UIImage imageNamed:@"home_bottom_tab_arrow"];
[_tabBarView addSubview:_selectView];
}
#pragma mark - UIButtonAction
-(void)btnAction:(UIButton *)button
{
//根据tag值判断当前的索引
self.selectedIndex = button.tag - 100;
//动画效果
[UIView animateWithDuration:0.2 animations:^{
_selectView.center = button.center;
} completion:nil];
}
2.标签栏 是否显示
//是否显示工具栏
- (void)showTabBar:(BOOL)show
{
CGRect frame = self.tabBarView.frame;
if (show)
{
frame.origin.x = 0;
}else
{
frame.origin.x = -kScreenWith;
}
//重新复制frame
[UIView animateWithDuration:0.2 animations:^{
self.tabBarView.frame = frame;
} completion:nil];
}
3.1 其他视图的工具栏是否显示
//拿回工具栏 - 视图将要出现的时候调用
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//拿回系统类型的控制器 再强制类型转成我们的类型
RootViewController *rootVc = (RootViewController *)self.tabBarController;
[rootVc showTabBar:YES];
}
3.2 隐藏工具栏
//初始化push按钮
- (void)initPushButton
{
UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pushButton.frame = CGRectMake(100, 100, 200, 40);
[pushButton addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
[pushButton setTitle:@"push" forState:UIControlStateNormal];
[self.view addSubview:pushButton];
}
- (void)pushAction
{
pushViewController *pushVC = [[pushViewController alloc] init];
[self.navigationController pushViewController:pushVC animated:YES];
**RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:NO];**
}