標籤:
在IOS7以前,UITabbarItem的圖片都是被固定渲染為藍色,想要改變UITabbarItem的圖片顏色就必須要自訂,在IOS7以後,得到了更新,方便大家自己去設定顏色,下面給出代碼!
1、建立UITabbarItem的預設圖片和選中圖片
//第一個介面 ChildViewController *childvc = [[ChildViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *childnav = [[UINavigationController alloc]initWithRootViewController:childvc]; // UITabBarItem *childItem = [[UITabBarItem alloc]init]; UITabBarItem *childItem = [[UITabBarItem alloc]initWithTitle:@"孩子情況" image:[UIImage imageNamed:@"關於孩子 預設效果.png"] tag:1]; childItem.selectedImage = [[UIImage imageNamed:@"關於孩子 選中效果.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; childnav.tabBarItem = childItem;
初始化的方法最好是用這個
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag;
其中
childItem.selectedImage = [[UIImage imageNamed:@"關於孩子 選中效果.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
這句話就是設定選中時候的圖片,始終讓圖片保持原樣不被渲染為藍色。
imageWithRenderingMode是UIImage的一個渲染模式,著色(Tint Color)是iOS7介面中的一個.設定UIImage的渲染模式:UIImage.renderingMode重大改變,你可以設定一個UIImage在渲染時是否使用當前視圖的Tint Color。UIImage新增了一個唯讀屬性:renderingMode,對應的還有一個新增方法:imageWithRenderingMode:,它使用UIImageRenderingMode枚舉值來設定圖片的renderingMode屬性。該枚舉中包含下列值:
- UIImageRenderingModeAutomatic // 根據圖片的使用環境和所處的繪圖上下文自動調整渲染模式。
- UIImageRenderingModeAlwaysOriginal // 始終繪製圖片原始狀態,不使用Tint Color。
- UIImageRenderingModeAlwaysTemplate // 始終根據Tint Color繪製圖片,忽略圖片的顏色資訊。
renderingMode屬性的預設值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取決於它顯示的位置。
2、設定UITabbarItem的title的顏色不被渲染
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:59.0/255.0 green:207.0/255.0 blue:202.0/255.0 alpha:1],UITextAttributeTextColor, nil]forState:UIControlStateSelected];
其中上面的那句是設定預設顏色的,下面的時設定選中後的字型的顏色。
下面是完整的AppDelegate.m方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //第一個介面 ChildViewController *childvc = [[ChildViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *childnav = [[UINavigationController alloc]initWithRootViewController:childvc]; // UITabBarItem *childItem = [[UITabBarItem alloc]init]; UITabBarItem *childItem = [[UITabBarItem alloc]initWithTitle:@"孩子情況" image:[UIImage imageNamed:@"關於孩子 預設效果.png"] tag:1]; childItem.selectedImage = [[UIImage imageNamed:@"關於孩子 選中效果.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; childnav.tabBarItem = childItem; //第二個介面 KKViewController *chatvc = [[KKViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *chatNav = [[UINavigationController alloc]initWithRootViewController:chatvc]; UITabBarItem *chatItem = [[UITabBarItem alloc]initWithTitle:@"聊天" image:[UIImage imageNamed:@"溝通 預設.png"] tag:2]; chatItem.selectedImage = [[UIImage imageNamed:@"溝通 選中.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; chatNav.tabBarItem = chatItem; //第三個介面 ToolsViewController *toolvc = [[ToolsViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *toolNav = [[UINavigationController alloc]initWithRootViewController:toolvc]; UITabBarItem *toolItem = [[UITabBarItem alloc]initWithTitle:@"工具列" image:[UIImage imageNamed:@"工具列 預設.png"] tag:3]; toolItem.selectedImage = [[UIImage imageNamed:@"工具列 選中.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; toolNav.tabBarItem = toolItem; //第四個介面 PersonViewController *loginvc = [[PersonViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *loginnav = [[UINavigationController alloc]initWithRootViewController:loginvc]; UITabBarItem *loginItem = [[UITabBarItem alloc]initWithTitle:@"個人中心" image:[UIImage imageNamed:@"個人中心 預設.png"] tag:4]; loginItem.selectedImage = [[UIImage imageNamed:@"個人中心 選中.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; loginnav.tabBarItem = loginItem; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:59.0/255.0 green:207.0/255.0 blue:202.0/255.0 alpha:1],UITextAttributeTextColor, nil]forState:UIControlStateSelected]; UITabBar *tb=[[UITabBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 96.0) ] ; [self.window addSubview:tb]; UITabBarController *tbc = [[UITabBarController alloc]init]; tbc.viewControllers = @[childnav,chatNav,toolNav,loginnav]; self.window.rootViewController = tbc; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}
有疑問的請留言相互交流!
IOS7以後無需自訂,改變UITabbarItem的圖片文字顏色