ios 三级控制器 标签栏

4 篇文章 0 订阅

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];**

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值