2013-09-24 111 views
7

我目前正在使用Xcode 5開發一個面向列表的應用程序。我有一個自定義色彩的標籤欄,自定義圖像的選項卡圖標,自定義色彩選項卡欄的圖標圖像時,但我找不到如何自定義圖標圖像的顏色,當它沒有被選中。現在它只是默認的灰色,你幾乎看不到我的綠色標籤欄。我想讓標籤欄圖標的圖像和名稱爲白色。自定義標籤欄圖標顏色

有人知道如何在Xcode 5中設置標籤欄圖標的圖像色調?

回答

17

你可以試試這個以色彩選擇的圖標:

// Custom the tab bar 
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]]; 

,這對着色非活動圖標:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]]; 
+2

注意setFinisheSelectedImage:withFinishedUnselectedImage在iOS的7 –

+0

馬修棄用如果在iOS的7棄用有另一種方式去這還是是,僅僅因爲蘋果希望保持灰色默認圖標的標準呢? – ian

+0

@ user2792129 - 是的,只是花了我幾分鐘讓我的答案輸入

26

你需要爲每個選項卡的(未選)圖像的渲染模式到UIImageRenderingModeAlwaysOriginal。因此,在您的應用程序委託中,獲取對選項卡欄的引用,然後遍歷每個選項欄欄項,調整圖像模式。

有可能是一個更好的方式來獲取到標籤欄的引用,但我做了以下內容:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
UITabBarController *tbc = [sb instantiateInitialViewController]; 
self.window.rootViewController = tbc; 
UITabBar *tb = tbc.tabBar; 

然後將圖像調整可以做如下:

NSArray *items = tb.items; 

for (UITabBarItem *tbi in items) { 
    UIImage *image = tbi.image; 
    tbi.selectedImage = image; 
    tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
} 
+0

謝謝馬修。我將該代碼放入我的.m應用程序委託中,它返回了一個說明「不支持的配置」的問題,「在導航項目Main.storyboard中不支持簡單樣式」。 – ian

+0

我的代碼假設標籤欄控制器是故事板中的初始視圖控制器。如果您的應用設置不同,那可能是問題的根源。 –

+0

這隻適用於其中一幅圖像。基本上我有一個未選中和選定的圖標。這個伎倆使未經選擇的工作,但選擇仍然只是一個藍色的混亂。 – Halsafar

5

試試這個way..it工作對我來說

在應用程序委託

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 


UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController); 
    UITabBar *tabBar=tabBarController.tabBar; 
    UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar 
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30 
} 

運行並去

4

設置與選定和非選擇圖像的自定義選項卡欄。 另外具有中心

UITabBar *tabBar = self.tabBarController.tabBar; 

UITabBarItem *item0 = [tabBar.items objectAtIndex:0]; 
UITabBarItem *item1 = [tabBar.items objectAtIndex:1]; 
UITabBarItem *item2 = [tabBar.items objectAtIndex:2]; 
UITabBarItem *item3 = [tabBar.items objectAtIndex:3]; 

[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ]; 
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-2.png"]]; 
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-3.png"]]; 
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-4.png"]]; 

item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0); 
item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0); 
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0); 
item3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0); 

**在第一視圖控制器的viewWillAppear中方法tabbarItem圖片插圖位置。 **

3

因爲setFinishedSelectedImage:

[item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ]; 

有:

[item0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; 
[item0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; 

有關更多信息,請參見UITabBarItem setFinishedSelectedImage: deprecated in iOS7 withFinishedUnselectedImage已過時,我通過更換使用拉姆小號的答案的修改過的版本。

+0

謝謝,你救了我兩天... –

5

如果你在可視化編輯器中有你的標籤欄,你可以在這裏完成。選擇標籤欄,並在「用戶自定義運行屬性」添加屬性: 關鍵路徑:selectedImageTintColor 類型:彩色 值:

+1

就像一個魅力。對於未選定的圖標是否還有一個美妙的魔力? –

6

你可以這樣做純粹是從故事板沒有通過添加「用戶自定義運行屬性編寫任何代碼「:

  1. 在故事板
  2. 打開選擇UITabViewController‘文檔大綱’,並確保您選擇‘場景中的標籤欄’視圖。
  3. 顯示「身份檢查員」。你應該看到了部分「用戶定義的屬性運行」
  4. 添加以下內容:
    • 關鍵路徑:tintColor
    • 類型:彩色
    • 值:選擇您想要的顏色。
+2

很高興知道。但用戶定義的運行時屬性是絕對的調試噩夢。 –

+1

除了tintColor,你還可以在Key Path中輸入什麼內容? –

+1

@RasmusBidstrup,所選IB項目的所有相關屬性。在我們的例子中是UITabBar。所以去蘋果文檔並閱讀UITabBar文檔。 – OhadM