我们在使用 UITabBarController 时,会给tabBarItem设置我们自己的图片和标题。但默认情况下,系统会进行渲染,选择tabBarItem的标题和图片会被渲染成蓝色,我们需要将被渲染过的tabBarItem修改成我们想要的样式。
不过,我们可以提取一个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"];当然,我们有很多个标题,希望可以一次设置所有的标题
+ (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]; }