在UITabBarController中显示图像 - 更新

时间:2013-12-09 12:56:25

标签: ios iphone objective-c uitabbarcontroller uitabbaritem

我想在我的UI中放置一个图像。作为UITabBarItem存在的ControllersUINavigationControllers。当我试图将图像放在它们上面时,结果看起来不太好。我只得到一半图像,图像没有颜色。

enter image description here

我使用的3张图片是尺寸为50X50的.png图片。

这是我用过的代码

self.custCareVC = [[CustomerCareViewController alloc] initWithNibName:@"CustomerCareViewController_iPhone" bundle:NULL];
        self.POController = [[PurchaeOrderViewController alloc] initWithNibName:@"PurchaeOrderViewController_iPhone" bundle:NULL];
        self.accAndContactsController = [[AccountsAndContactsViewController alloc] initWithNibName:@"AccountsAndContactsViewController_iPhone" bundle:NULL];

self.customerCareNavController = [[UINavigationController alloc] initWithRootViewController:self.custCareVC];
    self.customerCareNavController.title = @"Customer Service";

    self.purchaseOrderNavController = [[UINavigationController alloc] initWithRootViewController:self.POController];
    self.purchaseOrderNavController.title = @"PO";

    self.accAndContactsNavController = [[UINavigationController alloc] initWithRootViewController:self.accAndContactsController];
    self.accAndContactsNavController.title = @"Accounts And Contacts";

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.customerCareNavController, self.accAndContactsNavController, self.purchaseOrderNavController, nil];

    UIImage *selectedImage0     = [UIImage imageNamed:@"cust_serv_bw_selected.png"];
    UIImage *unselectedImage0   = [UIImage imageNamed:@"cust_serv_bw.png"];

    UIImage *selectedImage1     = [UIImage imageNamed:@"contacts_bw_selected.png"];
    UIImage *unselectedImage1   = [UIImage imageNamed:@"contacts_bw.png"];

    UIImage *selectedImage2     = [UIImage imageNamed:@"po_bw_selected.png"];
    UIImage *unselectedImage2   = [UIImage imageNamed:@"po_bw.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.image = unselectedImage0;
    item0.selectedImage = selectedImage0;

    item1.image = unselectedImage1;
    item1.selectedImage = selectedImage1;

    item2.image = unselectedImage2;
    item2.selectedImage = selectedImage2;

    self.tabBarController.selectedViewController = self.customerCareNavController;

我该如何纠正这个问题。为什么会这样?

3 个答案:

答案 0 :(得分:1)

我会在这里更精确地回答。

文档说标签栏图像通常是30x30,但我发现设置图像的最佳尺寸是48x32像素。这个尺寸仍然会呈现并为您提供更多空间。

图像是具有透明度的PNG,仅使用蒙版。 UI在未选中时呈现灰色,在选中时呈现蓝色/铬色。

如果您正在使用视网膜显示器,则需要添加两倍大小且名称为myimage@2x.png的图像。

如果您想更改项目的颜色,可以使用一些示例代码:Cocoa control TabBarController

答案 1 :(得分:0)

UINavigationController *lNavigationProgressController=[[UINavigationController alloc] initWithRootViewController:yourViewController];                                                       

[lNavigationProgressController.tabBarItem setImage:[UIImage imageNamed:@"yourImage.png"]];

答案 2 :(得分:0)

查看合适尺寸的图片:iOS Human Interface Guidelines: Icon and Image Sizes

如果您想使用更大尺寸的图像,请使用代码

    [item0 setImageInsets:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
每个tabBarItem

。这就像在HTML中使用填充一样。

要为图像和文本添加颜色,请使用代码段并根据需要进行更改:

    NSDictionary *attributeDictionary = [NSDictionary dictionaryWithObject:[UIColor darkGrayColor] forKey:UITextAttributeTextColor];
    [[UITabBarItem appearance] setTitleTextAttributes:attributeDictionary forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = UIColorFromRGBAlphaOne(0xBD1550);
    attributeDictionary = [NSDictionary dictionaryWithObject:titleHighlightedColor forKey:UITextAttributeTextColor];
    [[UITabBarItem appearance] setTitleTextAttributes:attributeDictionary forState:UIControlStateSelected];

这些方法很容易理解。