在項目中想實現自定義如圖UITabBarItem的選中狀態,一開始的想法是讓美工做了兩套圖,一個是未選中的灰色圖標,還有一套根據rgb自定義的圖標
樓主查了下
UITabBarItem *homeItem = [[UITabBarItem alloc] initWithTitle:@"首頁" image:[UIImage imageNamed:@"tab1"] tag:0];
[homeItem setFinishedSelectedImage:[UIImage imageNamed:@"tab1-1"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab1"]];
<pre name="code" class="objc">[homeItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:51/255.0 green:198/255.0 blue:165/255.0 alpha:1], UITextAttributeTextColor,
nil] forState:UIControlStateSelected];
通過此方法可以實現色調切換,但是項目出現警告,提示這兩個方法均已過時。ios7新增了兩個方法可以輕鬆實現
[[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:51/255.0 green:198/255.0 blue:165/255.0 alpha:1]];
[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:51/255.0 green:198/255.0 blue:165/255.0 alpha:1], UITextAttributeTextColor, nil]
forState: UIControlStateSelected];