2013-02-26 123 views
10

我用下面的代碼來創建圖像我UITabBarItemUITabBarItem圖像顏色是灰色的,而原始圖像的白色

self.tabBarItem.image = [UIImage imageNamed:@"tab_img.png"]; 

這tab_img.png包括黑色,白色和清晰顏色的。但在應用程序中,黑白圖像的所有部分變成灰色。我怎樣才能將這個灰色變成白色?

this is image that I use

enter image description here

+0

您可以在此處添加圖像嗎? – Sagrian 2013-02-26 10:12:21

+0

我已添加此主頁圖片。 – 2013-02-26 10:22:57

回答

3

圖片UITabBarItems應該只是alpha通道! 不透明的部分會顯示爲灰色(如果選擇藍色),但!

看看:http://devinsheaven.com/creating-uitabbar-uitoolbar-icons-in-adobe-illustrator/

+0

UITabBarItems的圖像可以是白色而不是灰色? – 2013-02-26 10:46:10

+0

https://github.com/rumex/RXCustomTabBar – blub 2013-02-26 11:11:14

+2

對於iOS 7+不適用。你可以使用' - imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal' – KPM 2014-09-25 20:29:15

0

我有同樣的問題一次,我用的圖像白色和阿爾法只有這樣image

我選擇self.tabBarItem.image = [UIImage imageNamed:@"Liste"];

+0

改變[UIImage imageNamed:@「tab_img.png」];改變[UIImage imageNamed:@「tab_img」];沒有幫助 – 2013-02-26 10:48:14

+0

我編輯了我的答案,以更清晰地標記 – pmk 2013-02-26 10:49:37

10

集和未選定的圖像設置。

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"mehr_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"mehr_icon"]]; 
+0

該方法在iOS 7中已棄用,在iOS 7或更高版本中使用@Cornel Damian方法。 – Henry 2014-06-30 09:17:07

+0

就是這樣,tks @fibnochi – 2014-09-24 08:12:28

0

唯一的辦法就是去IB(界面生成器),並在您的視圖控制器選擇UITabBarItem並進入「文件檢查器」向下滾動,你會看到全球色調她,你可以將其設置爲無顏色或您想要的任何顏色將對所選圖像生效。

按照下面的代碼是關注

setFinishedSelectedImage: withFinishedUnselectedImage:; 

這裏不再提供的iOS 7,而我們可以用

[yourCustomTabBarItem setSelectedImage:---]; 

但是這也將以此爲全球色調的色彩效果。

32

在iOS7,如果你使用IB你也可以繼承的UITabBarController再補充一點:

+ (void)initialize 
{ 
    //the color for the text for unselected tabs 
    [UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal]; 

    //the color for selected icon 
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];  
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
     for (UITabBarItem *tbi in self.tabBar.items) { 
      tbi.image = [tbi.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
     } 
    } 
} 
如果您創建的項目

manualy必須設置UIImageRenderingModeAlwaysOriginal上的每個圖標,然後從初始化添加代碼。

+0

好人。拯救了我的生命 – Liam 2014-05-21 09:35:35

+0

所選項目的文本也似乎受到第一行的影響。我不得不添加:[UITabBarItem.appearance setTitleTextAttributes:@ {NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];使選定的項目文本正確。 – 2014-07-29 20:29:25

+0

.....很好的回答) – BhushanVU 2014-12-12 11:37:46

1

如果您正在使用圖像資源,只需設置域「渲染爲」您的圖像(選中和未選中圖像)「原始圖像」(example

然後在你的廈門國際銀行設定的「圖像」和「選擇圖像「選項欄上的字段項目 (example