簡體   English   中英

如何以編程方式更改tabbarItem的圖像

[英]how to programmatically change the tabbarItem's image

我正在開發購物車標簽。 最初我只是使用默認徽章值來顯示底部標簽欄中購物車中的項目數。 現在設計師想要看上去,他想根據購物車中的物品數量顯示不同的圖像。 例如,如果有,請顯示cartTab-1.png,如果為2,則顯示cartTab-2.png ...

我試圖更改tabaritem( UITabBarItem )的圖像,但它對我不起作用。 這可行嗎? 我與我的同事討論過,他說我可能要自己在tabbarItem上繪制圖像。 你有什么建議嗎? 謝謝

更多細節:

  1. 我使用InterfaceBuilder創建了tabItem,並在那里設置了圖像和標題
  2. 我需要支持ios4。 所以我不能使用setSelectedImage ...
  3. 在我的情況下,它是一個KVO,如果購物車數量改變,它會通知方法更新圖像。 不在初始化步驟中。

有誰知道為什么[self.tabBarItem setImage:[UIImage imageNamed:@"cartxxx.png"]]不起作用? 當我調試時,屬性確實發生了變化,但UI保持不變

更新

以下代碼有效。 感謝大家!

UIImage* cartTabImage = [UIImage imageNamed:cartTabImageName];
[[self.tabBarController.tabBar.items objectAtIndex:3] setImage:cartTabImage];

適用於2個標簽的Swift 3.0版本,

self.tabBar.items?[0].image = UIImage(named: "inactive_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[0].selectedImage = UIImage(named: "active_image_0")?.withRenderingMode(.alwaysOriginal)

self.tabBar.items?[1].image = UIImage(named: "inactive_image_1")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].selectedImage = UIImage(named: "active_image_1")?.withRenderingMode(.alwaysOriginal)
UITabBarItem *tabBarItem0 = [self.tabBarController.tabBar.items objectAtIndex:0];

[tabBarItem0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

我找到的最簡單的方法是

     self.tabBarItem.image=[UIImage imageNamed:@"myImage.png"];

嘗試這個:

int numItems = 0; // count of items in your shopping cart
NSString *imageName = [NSString stringWithFormat:@"cartTab-%d",numItems];

// change your image
[[self.tabBar.items objectAtIndex:myIndex] setImage:[UIImage imageNamed:imageName]];

// or, if you want to set it when initializing the tabBar
UITabBarItem *item = [[[UITabBarItem alloc] initWithTitle:myTitle image:[UIImage imageNamed:imageName] tag:someTag];

這個答案可能會幫助你

  UITabBarItem *i5=[[UITabBarItem alloc]initWithTitle:@"Profile" image:[UIImage imageNamed:@"profile.png"] tag:5];
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage

當用戶選擇了選項卡時,將顯示selectedImage。 當用戶選擇了不同的選項卡時,將顯示unselectedImage。

在你的viewDidLoad:

UIImage *c1 = [UIImage imageNamed:@"cart1.png"];
UIImage *c2 = [UIImage imageNamed:@"cart1unselected.png"];
[[self tabBarItem] setFinishedSelectedImage:c1 withFinishedUnselectedImage:c2];

隨着更新的問題以及其他的答案中提到,在大多數情況下, UITabBarItem必須通過訪問UITabBarController直接。

似乎iOS創建了UITabBarItem實例的副本,這解釋了為什么更新self.tabBarItem屬性不會反映用戶界面中的更改:

兩個UITabBarItem實例

我的猜測是,當Tab Bar項目是以編程方式而不是故事板創建的時候會發生這種情況,但這只是猜測。

然后,正如所指出的那樣,解決方案是通過Tab Bar控制器訪問Tab Bar項目數組。 這個解決方案很糟糕,因為它取決於標簽欄項目索引的知識:

UITabBarItem *tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem setImage:image];
[tabBarItem setSelectedImage:image];

不要忘記更新默認和選定狀態的圖像。

let favorites = UITabBarItem(title: nil, image:UIImage(named: "Screen Shot 2018-12-13 at 11.00.42 AM") , tag: 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM