2014-04-16 43 views
1

使用此代碼的TabBar圖標像素化

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
    UITabBar *tabBar = tabBarController.tabBar; 
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; 
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2]; 
    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3]; 

    tabBarItem1.title = @"Home"; 
    tabBarItem2.title = @"Maps"; 
    tabBarItem3.title = @"My Plan"; 
    tabBarItem4.title = @"Settings"; 

    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]]; 
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]]; 
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"myplan_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"myplan.png"]]; 
    [tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]]; 

工作fine..however,當我使用這個圖片,它看起來真實iphone testing..using較大(在寬度和高度)圖像慣於適合像素化因爲只有它的一部分被看到。有沒有一種方法可以將這些較大的圖像伸展到特定的寬度和高度,以便看到它的整個部分?

+0

你能提供.png文件的尺寸是多少? – PCoder123

+0

它的30x30px ..我想用一個大一點的東西在90x90左右,所以它不會看起來像素化 – user3517855

+0

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconMatrix.html – PCoder123

回答

2

請遵循 iOS Human Interface Guidelines 中列出的尺寸。此外,請勿指定.png,因爲您希望能夠解釋非視網膜和視網膜圖像(w/@ 2x)。

+0

什麼是你的意思是不指定.png? – user3517855

+1

只要[UIImage imageNamed:@「yourName」]而不是[UIImage imageNamed:@「yourName.png」] –

0

在iOS 6中或更早的版本,如果你想創建一個32×32大小的按鈕,你會想辦法讓這些像素尺寸的PNG,並將其命名爲Button.png

然後,當視網膜屏幕來了,你開始需要製作2個資產 Button.png AND [email protected] @ 2x資產爲64x64像素。

使用iOS7和更高版本,不需要同時創建兩個資產。您只需使用Retina @ 2x資產。

將您的所有圖像資產放入資產目錄中可以更輕鬆。所以當你從代碼中使用@ 2x資源時,你不應該寫.png擴展名。

例如:

[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected"]... 

注意:當你從故事板中引用的資產@ 2倍的資產,你只寫了資產的名稱,而不@ 2倍。

例如:文件名爲[email protected],在StoryBoard你會寫home_selected.png