iOS 13.0如何修改uitabbar 的titlecolor

2020-04-20  本文已影响0人  tp夕阳武士

iOS升级到13.0以后,tabbar的标签文字颜色修改有了变化,变化后的解决方案如下:

UITabBarController *tb = [[UITabBarController alloc] init];
    RFNewController *rvc = [RFNewController new];
    RFPresentVC *rvc2 = [RFPresentVC new];
    [tb addChildViewController:rvc];
    [tb addChildViewController:rvc2];
    rvc.title = @"控制器1";
    rvc2.title = @"控制器2";
    

    if (@available(ios 13.0,*)) {
        UIColor *unselectColor = [UIColor redColor];
        UIColor *selectColor = [UIColor blueColor];
        
        UITabBarAppearance *appearance = UITabBarAppearance.new;
        NSMutableParagraphStyle *par = [[NSMutableParagraphStyle alloc]init];
        par.alignment = NSTextAlignmentCenter;
        UITabBarItemStateAppearance *normal = appearance.stackedLayoutAppearance.normal;
        if(normal) {
            normal.titleTextAttributes = @{NSForegroundColorAttributeName:unselectColor,NSParagraphStyleAttributeName : par};
        }

        UITabBarItemStateAppearance *selected = appearance.stackedLayoutAppearance.selected;
        if(selected) {
            selected.titleTextAttributes = @{NSForegroundColorAttributeName:selectColor,NSParagraphStyleAttributeName : par};
        }
        tb.tabBar.standardAppearance = appearance;
    }else{
        UIColor *unselectColor = [UIColor redColor];
        UIColor *selectColor = [UIColor blueColor];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:unselectColor} forState:UIControlStateNormal]; // 设置未选中颜色
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor} forState:UIControlStateSelected];
    }

效果图:


WechatIMG12.jpeg
上一篇 下一篇

猜你喜欢

热点阅读