UITabbarItem的背景色和文字的设置

1.UITabbarItem的背景色的设置。

tabBar.tabBar.selectionIndicatorImage = [UIImage imageWithColor:[UIColor colorWithRed:0.27 green:0.73 blue:0.98 alpha:1]];
// 下面是UIImage的分类里的方法
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, Screen_Width/5, 49); //宽高 1.0只要有值就够了
    UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
    CGContextFillRect(context, rect);//用这个颜色填充这个上下文
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
    UIGraphicsEndImageContext();
    
    return image;
}

2.文字的设置(选中状态和正常状态)

[viewController1.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor blackColor], UITextAttributeTextColor,
                                            nil] forState:UIControlStateNormal]; [viewController1.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                           [UIColor whiteColor], UITextAttributeTextColor,
                                            nil] forState:UIControlStateSelected];

你可能感兴趣的:(UITabbarItem的背景色和文字的设置)