美文网首页
iOS 13.0如何修改uitabbar 的titlecolor

iOS 13.0如何修改uitabbar 的titlecolor

作者: tp夕阳武士 | 来源:发表于2020-04-20 21:23 被阅读0次

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

相关文章

网友评论

      本文标题:iOS 13.0如何修改uitabbar 的titlecolor

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