TabBar的适配 (持续更新)

987 阅读1分钟

在iOS 13和 iOS 15系统下,tabbar也发生了一些更新变化,在使用的时候要特别注意,下面是一些简单的关于tabbar的背景色,以及字体颜色的设置代码:

    //设置背景颜色
    if(@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
        appearance.backgroundImage = [UIImage imageWithColor: [UIColor whiteColor]];
        appearance.shadowImage = [UIImage imageWithColor:[UIColor whiteColor]];
        //下面这行代码最关键
        //不加下面的判断,在正常模式下,tabbar的背景颜色为灰色
        if (KCustomerInfo.darkMode) {
            [appearance configureWithTransparentBackground];
        }else {
            [appearance configureWithOpaqueBackground];
        }
        
        self.tabBar.standardAppearance = appearance;
        //不加如下代码,在点击切换item或者滑动的时候背景颜色会发生变化
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = [self.tabBar standardAppearance];
        } else {
            // Fallback on earlier versions
        };

    }else{
        [self.tabBar setBackgroundImage:[UIImage imageWithColor:KCustomerInfo.darkMode ? UIColorFromRGB(0x333333):[UIColor whiteColor]]];
        [self.tabBar setShadowImage:[UIImage imageWithColor:KCustomerInfo.darkMode ? UIColorFromRGB(0x333333):[UIColor whiteColor]]];
        self.tabBar.translucent = YES;
    }
    
    //添加横线
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, kScreenWidth, 0.5)];
    view.backgroundColor = kGrayGroundColor;
    [self.tabBar insertSubview:view atIndex:0];

    //设置字体颜色
    if (@available(iOS 13.0, *)) {
        self.tabBar.tintColor = KCustomerInfo.darkMode?[UIColor whiteColor]:[UIColor blackColor];
        self.tabBar.unselectedItemTintColor = UIColorFromRGB(0x666666);
    }else {
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName:KCustomerInfo.darkMode?[UIColor whiteColor]:[UIColor blackColor]} forState:UIControlStateSelected];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x666666)} forState:UIControlStateNormal];
    }