tabBarItem标题与图片去除渲染

我们在使用 UITabBarController 时,会给tabBarItem设置我们自己的图片和标题。但默认情况下,系统会进行渲染,选择tabBarItem的标题和图片会被渲染成蓝色,我们需要将被渲染过的tabBarItem修改成我们想要的样式。

设置子控制器

// 设置子控制器
EssenceViewController *essenceVC = [[EssenceViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:essenceVC];

// 这个self其实就是UITabBarController,我将其提取了出来封装了个BaseTabBarController,对其子控制器进行管理
[self addChildViewController:nav1];

去除图片渲染

UIImage *image = [UIImage imageNamed:@"imageName"];
// 去除渲染
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.selectedImage = image;

不过,我们可以提取一个Category

// UIImage+image.h
@interface UIImage (image)

+ (UIImage *)imageOriginalWithName:(NSString *)imageName;

@end


// UIImage+image.m
@implementation UIImage (image)

+ (UIImage *)imageOriginalWithName:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

@end

使用时

#import "UIImage+image.h"

nav1.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_essence_click_icon"];

去除标题渲染

NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil];

[nav1.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateSelected];

当然,我们有很多个标题,希望可以一次设置所有的标题

+ (void)load
{
    // 获取某个类中的UITabBarItem
    // 这个self其实就是UITabBarController,我将其提取了出来封装了个BaseTabBarController,对其子控制器进行管理
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];

    // 修改title的文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil];
    [item setTitleTextAttributes:attrs forState:UIControlStateSelected];

    // 设置字体尺寸:只有设置正常状态下,才会有效果
    NSMutableDictionary *attrsNor = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13.0],NSFontAttributeName, nil];
    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值