2013-12-09 65 views
1

我想将图像放入我的UI中。作为UITabBarItem存在的ControllersUINavigationControllers。当我试图把图像放在他们身上时,结果看起来不太好。我只获得了一半图像,图像没有显示颜色。在UITabBarController中显示图像 - 已更新

enter image description here

,我已经使用的具有png格式尺寸50X50图像3倍的图像。

在这里,我已经使用

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; 

我怎样才能纠正这种代码。这是为什么发生?

+1

你是否使用了sufix @ 2x的高清图像? – Pull

+0

没有..我没有使用@ 2X ..会试试这个,让你知道 – Shradha

+0

我用@ 2x现在和尝试与30x30和50x50图像..但仍然无法工作 – Shradha

回答

1

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

该文档说标签栏图像通常是30x30,但我发现设置图像的最佳尺寸是48x32像素。这个大小仍然呈现并给你更多的空间。

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

如果您使用的是视网膜显示器,则需要添加两倍大小的图像,名称为:[email protected]

如果你想改变项目的颜色,有一些示例代码:Cocoa control TabBarController

+0

不..我没有使用@ 2x。 。会试试这个,让你知道 – Shradha

+0

应该可能解决你的问题:) – Pull

+0

我加了@ 2x,结果我得到了更新在我的问题..仍然没有得到我所需要的.. – Shradha

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

[lNavigationProgressController.tabBarItem setImage:[UIImage imageNamed:@"yourImage.png"]]; 
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]; 

的方法是通过名字很容易理解的。

+0

但是使用'setImageInset'不是一个好方法,因为图像将在非ratina设备中进行像素化。如链接所述,使用尺寸合适的图片是一种很好的标准方法。 –