iOS 更新tabbar 皮肤

核心方法

通过调用 Vc.tabBarItem.image 来更新皮肤包
Vc.tabBarItem.image = image;
Vc.tabBarItem.selectedImage = selImage;
Vc.tabBarItem.title = title;

    Vc.tabBarItem.image = image;
    Vc.tabBarItem.selectedImage = selImage;
    Vc.tabBarItem.title = title;

添加通知方法

该方法在切换皮肤之后调用

-(void)configureHolidaySkin{
    UIImage *home_normal = [[TPSkinManager shareManager]tabBarImageNormalAutoMatch:TPSkinMapKey_tabBar_home];
    UIImage *home_selected = [[TPSkinManager shareManager]tabBarImageSelectedAutoMatch:TPSkinMapKey_tabBar_home];
    
    UIImage *video_normal = [[TPSkinManager shareManager]tabBarImageNormalAutoMatch:TPSkinMapKey_tabBar_video];
    UIImage *video_selected = [[TPSkinManager shareManager]tabBarImageSelectedAutoMatch:TPSkinMapKey_tabBar_video];
    
    UIImage *ask_normal = [[TPSkinManager shareManager]tabBarImageNormalAutoMatch:TPSkinMapKey_tabBar_ask];
    UIImage *ask_selected = [[TPSkinManager shareManager]tabBarImageSelectedAutoMatch:TPSkinMapKey_tabBar_ask];
    
    UIImage *wenba_normal = [[TPSkinManager shareManager]tabBarImageNormalAutoMatch:TPSkinMapKey_tabBar_wenba];
    UIImage *wenba_selected = [[TPSkinManager shareManager]tabBarImageSelectedAutoMatch:TPSkinMapKey_tabBar_wenba];
    
    UIImage *my_normal = [[TPSkinManager shareManager]tabBarImageNormalAutoMatch:TPSkinMapKey_tabBar_my];
    UIImage *my_selected = [[TPSkinManager shareManager]tabBarImageSelectedAutoMatch:TPSkinMapKey_tabBar_my];
    
    [self setUpOneViewController:_sideViewController WithImage:[home_normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithSelImage:[home_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithTitle:@"首页"];
    [self setUpOneViewController:videoBar WithImage:[video_normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithSelImage:[video_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithTitle:@"视频"];
    [self setUpOneViewController:paperNumBar WithImage:[ask_normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithSelImage:[ask_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithTitle:@"澎湃号"];
    [self setUpOneViewController:paperCircleBar WithImage:[wenba_normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithSelImage:[wenba_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] WithTitle:@"澎友圈"];
    [self setUpOneViewController:myBar
                       WithImage:[my_normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                    WithSelImage:[my_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                       WithTitle:@"我的"];
    self.tabBar.tintColor =  [[TPSkinManager shareManager]colorAutoMatch:TPSkinMapKey_tabBar_titleSelectedColor];
    
    //这里是设置tabbar 整体背景图
    if ([[TPSolartermThemeManager shareInstance] couldShowSolarterm]) {
        [self.tabBar insertSubview:self.backgroundImgView atIndex:0];
        self.backgroundImgView.image = [[TPSolartermThemeManager shareInstance] imageNamedAutoMatch:@""];
        self.backgroundImgView.frame = self.tabBar.bounds;
    } else {
        [self.backgroundImgView removeFromSuperview];
    }
    
    //设置图片大小
    [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITabBarItem *item = obj.tabBarItem;
        if (![[TPSolartermThemeManager shareInstance] couldShowSolarterm] ||
            [TPUserDefault instance].isNightMode.integerValue == 1) {
            item.imageInsets = UIEdgeInsetsZero;
        } else {
            item.imageInsets = UIEdgeInsetsMake(-20, - 20, 0, - 20);
        }
    }];
}

实现修改tabbar 方法

- (void)setUpOneViewController :(UIViewController *)Vc WithImage:(UIImage *)image WithSelImage:(UIImage *)selImage WithTitle:(NSString *)title{
    
    Vc.tabBarItem.image = image;
    Vc.tabBarItem.selectedImage = selImage;
    Vc.tabBarItem.title = title;
    
    //正常
    NSMutableDictionary *normalDict = [NSMutableDictionary dictionary];
    normalDict[NSFontAttributeName] = appFont(10*PLUS_SCALE, NO);
    normalDict[NSForegroundColorAttributeName] = [[TPSkinManager shareManager]colorAutoMatch:TPSkinMapKey_tabBar_titleNormalColor];
    [Vc.tabBarItem setTitleTextAttributes:normalDict forState:UIControlStateNormal];
    //选中
    NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary];
    selectedDict[NSForegroundColorAttributeName] =  [[TPSkinManager shareManager]colorAutoMatch:TPSkinMapKey_tabBar_titleSelectedColor];
    [Vc.tabBarItem setTitleTextAttributes:selectedDict forState:UIControlStateSelected];
    self.tabBar.tintColor = [[TPSkinManager shareManager]colorAutoMatch:TPSkinMapKey_tabBar_titleSelectedColor];
    if (@available(iOS 13.0, *)) {
        /*ios 13 及以上的时候,
         1 如果 setTitleTextAttributes 中没有设置字体颜色
         这里设置的字体颜色就会有其作用,
         2 如果 setTitleTextAttributes 有设置字体颜色,必须给 self.tabBar.unselectedItemTintColor 设置一个
         颜色,未选中字颜色才会起作用(但这里起作用的颜色是setTitleTextAttributes),否则会展示一个系统默认的灰色字体;
        */
        /*
         ios 13 以前的只需要设置 setTitleTextAttributes 即可设置字体颜色
         */
        self.tabBar.unselectedItemTintColor = [[TPSkinManager shareManager]colorAutoMatch:TPSkinMapKey_tabBar_titleNormalColor];
    }
}

你可能感兴趣的:(ios)