tabBarItem标题与图片去除渲染

    xiaoxiao2021-03-25  9

    我们在使用 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]; }
    转载请注明原文地址: https://ju.6miu.com/read-149832.html

    最新回复(0)