2012-01-04 44 views
1

我想改變我的應用程序中的選項卡欄的圖像。當我改變圖像時,它給我一個空白圖像。如何在標籤欄控制器上設置圖像?

  • 我可以設置.png格式圖片嗎?
  • 是否可以更改標籤欄的顏色?
  • 我可以在標籤欄中使用彩色圖像嗎?

回答

3

不,您可以使用.icon圖像作爲具有透明背景和黑白色的tabbar。也可以設置tabbar的顏色。

的TabBar顏色

 CGRect frame = CGRectMake(0.0, 0, 320, 48); 
UIView *v = [[UIView alloc] initWithFrame:frame]; 
[v setBackgroundColor:[UIColor colorWithRed:0.2 green:0.8 blue:0.4 alpha:0.3]]; 
//[v setAlpha:1.0]; 
[[tabbar tabBar] insertSubview:v atIndex:0]; 
[v release]; 
1

您通常會設置圖像在AppDelegate類的標籤欄。它可能具有被定義爲_tabBarController標籤欄控制器,所以代碼將是:

[[_tabBarController tabBar] setBackgroundImage:[UIImage imageNamed:@"imageName.png"]]; 

如果所提供的圖像是適當的大小或伸展的,它將是圖像的,否則這將是圖像平鋪然而,許多它需要填寫標籤欄。

如果您願意,也可以更改標籤顏色。

3

在iOS5中所有這些東西都是可能的!

UIImage *selectedImage0 = [UIImage imageNamed:@"TabBa1selected.png"]; 
    UIImage *selectedImage1 = [UIImage imageNamed:@"TabBa2selected.png.png"]; 
    UIImage *selectedImage2 = [UIImage imageNamed:@"TabBa3selected.png.png"]; 

    UITabBar *tabBar = self.tabBarController.tabBar; 
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0]; 
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1]; 
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2]; 

    [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:selectedImage0]; 
    [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:selectedImage1]; 
    [item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:selectedImage2]; 
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabBarBackground.png"]; 
    UIImage* tabBarSelected = [UIImage imageNamed:@"SelectedImage.png"]; 
    [[UITabBar appearance] setBackgroundImage:tabBarBackground]; 
    [[UITabBar appearance] setSelectionIndicatorImage:tabBarSelected]; 
+0

您還可以將未選中的圖像設置爲不同的圖像。在這種情況下,我只是將它們設置爲相同。 – 2012-01-04 06:27:45

相關問題