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透明,可以設置一張透明的圖片進來,即可。