美文网首页
tabbar效果切换

tabbar效果切换

作者: 数字d | 来源:发表于2020-01-15 17:21 被阅读0次
1

如图,如果点击了我的,图标变成绿色,当切换成企业用户,”我的“图标变成”企业“图标。

在网上搜索出来的方法有些点击出现闪退,或者是出现非选中字体空白,还有会出现tabbar页面push页面之后返回会出现navigationbar双层效果,最后只好自己写了。

切换按钮事件:

1.缓存当前需要展示的状态[企业账户或我的]
2.发送通知,重新执行tabbar初始化
3.在tabbar初始化的时候从缓存中获取要展示的状态

核心代码

tabbar初始化代码

    ChargeStationViewCtl *chargeVc = [[ChargeStationViewCtl alloc] init];
    chargeVc.view.backgroundColor = [UIColor whiteColor];
    [self addChildVc:chargeVc title:@"充电站" image:@"bar_icon_charge_nor" selectedImage:@"bar_icon_charge_sel"];
    
    
    SCCharingCtl * scVc = [[SCCharingCtl alloc] init];
    scVc.view.backgroundColor =[UIColor whiteColor];
    [self addChildVc:scVc title:@"扫码充电" image:@"bar_icon_scan_nor" selectedImage:@"bar_icon_scan_sel"];
    
    
  //  获取缓存中的展示状态
       NSString * switcType = GETDEFAULTS(HP_SWITCH_TYPE);
       if ([HPUtils rq_isEmpty:switcType]) {
           SAVEDEFAULTS(@"person", HP_SWITCH_TYPE);
           PersonInfoViewCtl * infoVc = [[PersonInfoViewCtl alloc] init];
             infoVc.view.backgroundColor = [UIColor whiteColor];
             [self addChildVc:infoVc title:@"我的" image:@"bar_icon_my_nor" selectedImage:@"bar_icon_my_sel"];
       }else
       {
           if ([switcType isEqualToString:@"person"]) {
               SAVEDEFAULTS(@"person", HP_SWITCH_TYPE);
               PersonInfoViewCtl * infoVc = [[PersonInfoViewCtl alloc] init];
                 infoVc.view.backgroundColor = [UIColor whiteColor];
                 [self addChildVc:infoVc title:@"我的" image:@"bar_icon_my_nor" selectedImage:@"bar_icon_my_sel"];
           }else
           {
               SAVEDEFAULTS(@"enterprise", HP_SWITCH_TYPE);
               PersonInfoViewCtl * infoVc = [[PersonInfoViewCtl alloc] init];
               infoVc.view.backgroundColor = [UIColor whiteColor];
               [self addChildVc:infoVc title:@"企业账户" image:@"bar_icon_company_nor" selectedImage:@"bar_icon_company_sel"];
//               [self setViewControllers:@[self.viewControllers[0],self.viewControllers[1],infoVc]  animated:NO];
           }
       }


 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chageItemInfoThreeToPerson) name:HP_NOTI_TOPERSON object:nil];
    
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeItemInfoThreeToCompany) name:HP_NOTI_ENterPrise object:nil];


- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    childVc.title = title;
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

    
    if (@available(iOS 13, *)){
        [[UITabBar appearance] setUnselectedItemTintColor:COLOR(179, 179, 179, 1)];
        [[UITabBar appearance] setTintColor:COLOR(0, 224, 169, 1)];
    }else
    {
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
        selectTextAttrs[NSForegroundColorAttributeName] = COLOR(0, 224, 169, 1);
        textAttrs[NSForegroundColorAttributeName] = COLOR(179, 179, 179, 1);
        [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    }

    HPNaviGationCtl *nav = [[HPNaviGationCtl alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
}
    

通知方法实现

-(void)changeItemInfoThreeToCompany
{
    NSMutableArray *vcArray = [NSMutableArray arrayWithObjects:self.childViewControllers[0],self.childViewControllers[1] , nil];
    
    PersonInfoViewCtl * infoVc = [[PersonInfoViewCtl alloc] init];
    infoVc.view.backgroundColor = [UIColor whiteColor];
   HPNaviGationCtl * nav =  [self defaultaddChildVc:infoVc title:@"企业账户" image:@"bar_icon_company_nor" selectedImage:@"bar_icon_company_sel"];
    [vcArray addObject:nav];
    [self setViewControllers:vcArray  animated:NO];
    
}

-(void)chageItemInfoThreeToPerson
{
  
    NSMutableArray *vcArray = [NSMutableArray arrayWithObjects:self.childViewControllers[0],self.childViewControllers[1] , nil];
      
      PersonInfoViewCtl * infoVc = [[PersonInfoViewCtl alloc] init];
      infoVc.view.backgroundColor = [UIColor whiteColor];
    HPNaviGationCtl * nav =  [self defaultaddChildVc:infoVc title:@"我的" image:@"bar_icon_my_nor" selectedImage:@"bar_icon_my_sel"];
      [vcArray addObject:nav];
      [self setViewControllers:vcArray  animated:NO];

}

相关文章

网友评论

      本文标题:tabbar效果切换

      本文链接:https://www.haomeiwen.com/subject/kisractx.html