UITabBar-UITabBarItem图片的背景颜色属性和文字的颜色大小设置
UITabBarItem设置的图片选中状态下默认的是蓝色,如何改变它的颜色为图片自带的颜色呢?
typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
// 使用图像的上下文中使用的默认渲染模式,使用默认的蓝色
UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used
// 总是画出原始图像,使用图片自带的颜色
UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template
// 总是把图像作为模板图像,忽略了它的颜色信息
UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information
} NS_ENUM_AVAILABLE_IOS(7_0);
使用 imageWithRenderingMode 设置 如下:
UINavigationController *navView = [sb instantiateInitialViewController]; navView.tabBarItem.title = @"扫描二维码"; navView.tabBarItem.image = [UIImage imageNamed:@"01"]; navView.tabBarItem.selectedImage = [UIImage imageNamed:@"01h"]; UINavigationController *navScan = [[UINavigationController alloc] initWithRootViewController:scanVC]; navScan.tabBarItem.title = @"冲击波扫描"; navScan.tabBarItem.image = [[UIImage imageNamed:@"02"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; navScan.tabBarItem.selectedImage = [[UIImage imageNamed:@"02h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
---------------------------------------------------------------------------------------------------------------------------
// 改变标题字体颜色和大小
// Font Family: Helvetica(Helvetica-Oblique,Helvetica-BoldOblique,Helvetica,Helvetica-Bold) iOS下 默认字体
方法一:
- (void)viewDidLoad { [super viewDidLoad]; // 改变标题字体颜色和大小 [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:12.0f], NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateSelected];
// self.tabBar.tintColor = [UIColor redColor]; }
方法二:
[navScan.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateNormal];
[navScan.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
如果使用self.tabBar.tintColor设置 图片的颜色也会跟着变。
使用方法一,会将所有的tabBarItem 颜色和字体大小全部改变。
使用方法二,只会当前的navScan.tabBarItem 颜色和字体大小改变。
设置tabBar的背景颜色,以下设置是不管用的
@implementation WETabBarController - (void)viewDidLoad { [super viewDidLoad]; self.tabBar.backgroundColor = [UIColor redColor];
}
如果想要改变tabBar的背景 可以设置它的背景图片
self.tabBar.backgroundImage = [UIImage imageNamed:@"redImage"];
如果要tabBar透明,可以设置一张透明的图片进来,即可。