2011-12-10 159 views
2

我試圖讓我的自定義視網膜圖像顯示在我的自定義iOS5 TabBar中失敗。將自定義視網膜圖像添加到TabBar在iOS5

我在我的TabBar中有4個項目,我已將選定/未選中的圖像設置爲[email protected],其分辨率爲160px x 75px。我認爲這些160像素寬度圖像中的4個可以很好地適應640像素的視網膜寬度。

您可以查看這裏我[email protected] https://lh4.googleusercontent.com/-afHulbEcxNE/TuPe-YIj91I/AAAAAAAAAII/lCojphAxF9w/s160/contact%2525402x.png

我已經設定程式設計的所有項目,如下圖所示。

UIImage *selectedContact = [UIImage imageNamed:@"[email protected]"]; 
UIImage *unselectedContact = [UIImage imageNamed:@"[email protected]"]; 

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:selectedContact withFinishedUnselectedImage:unselectedContact]; 
[item1 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact]; 
[item2 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact]; 
[item3 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact]; 

在運行時,我可以看到,比例設置爲1

爲什麼不是2被拾起掉圖像的後綴?標籤欄很大,並沒有縮放。

請參閱下面的屏幕截圖模擬器......我的應用程序圖標,默認圖標

https://lh5.googleusercontent.com/-A5oxZprlDhU/TuPfAlG_HQI/AAAAAAAAAIc/mIwHXOPZSrE/s735/simulator.png

我的其他視網膜圖像工作。

想法?我正在駕駛自己堅果☺預先感謝。

回答

2

您是否查看了定製UITabBar外觀的文檔?

您應該通過Ray Wenderlich瞭解關於使用UIAppearance API的教程。

他的代碼是這樣的:

UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 
[[UITabBar appearance] setBackgroundImage:tabBackground]; 
[[UITabBar appearance] setSelectionIndicatorImage: [UIImage imageNamed:@"tab_select_indicator"]];` 

注意,定製發生在標籤欄上,而不是標籤欄項目,假設它爲每一個相同的定製。

作爲nbransby said您不要在文件名中使用@ 2x。

之前未經編輯的答案:


從文檔:

標籤欄上顯示的圖像從該圖像導出。如果此圖片太大而無法放在標籤欄上,則會縮放以適合它。標籤欄圖像的大小通常爲30 x 30點。源圖像中的alpha值用於創建未選中和選定的圖像 - 不透明值將被忽略。

您的圖標應該是正常分辨率的30x30像素和視網膜分辨率的60x60像素。他們也應該是一個純色,標籤欄添加着色。

+0

這是圖標壽,iOS5之前? – Chris

+0

標籤欄圖標,您可以開發的每個iOS版本。 – JoePasq

+0

我的歉意,但我不跟隨。我在HIG的印象下,現在我可以選擇在圖標上指定圖像。從HIG,我認爲如果@ 2x指令存在,API會擴展嗎? – Chris

5

JoePasq是錯的,他顯然沒有及時瞭解最新的iOS5外觀定製API。您的問題可能是您在文件名中指定了@ 2x,只需將「contact.png」傳遞給imagedNamed ...

[UIImage imageNamed:@「contact.png」]

+0

哇,他在談論選擇圖像,我在談論圖標。我走了。我會更新我的答案。謝謝。 – JoePasq

+0

您對文件名加載是否正確。從[docs](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006890-CH3-SW10):> 「如果屏幕的縮放比例爲2.0,則此方法首先搜索具有相同文件名的圖像文件,並附加@ 2x後綴。例如,如果文件的名稱是按鈕,它首先搜索按鈕@ 2x。「 – JoePasq

+0

該命令應該是:[UIImage imageNamed:@」contact「]; .png不是必需的。 – Linasses