2015-10-16 58 views
1

我有一個帶有5個選項卡的選項卡欄。我已將selectedunselected狀態的tab bar items的狀態設置爲不同的圖像。TabBar項色調不變

無論我做什麼,色調都不會改變,並且不會改變圖像的顏色。

選中標籤時,顏色應該是黑色,未選中時應該是橙色。

以下是分配圖像的屬性檢查器的圖像。

enter image description here

標籤欄的圖片

enter image description here

如何改變圖像的色彩?

回答

0

問題是您無法控制未選定項目的色調顏色。這不是你的代碼的錯誤;這只是iOS的工作原理。這使用是可能的,但在某些時候(iOS 7?不記得)它只是消失。

因此,您的屏幕截圖中發生的事情是,您已將選定的色調設置爲橙色,這就是結束了。一個標籤欄項目是選擇,它是橙色。

+0

你是對的。那就是發生了什麼事。現在我該如何糾正它? –

+0

您不能使用有色圖像。你將不得不使用一個沒有着色的'image'和一個沒有着色的'selectedImage',現在這些圖像的顏色(和透明度)將被直接使用。 – matt

+1

截至ios 10+,你可以用你的app delegate中的'tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray' – Anchor

0

其中一個解決方案是提供兩組標籤圖標。有一個帖子,這是非常相似,你的情況,你可以看看它: Custom tab bar icon colors

我覺得這個代碼(由Tunvir拉赫曼Tusher)是很好的解釋:

UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar 
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30 
0

如果你是爲IOS 10或更新版本開發,您可以更改未選中的色調顏色,在舊版本中,您只能更改所選的tintColor;這是一個實現:

1)進入的appDelegate /應用程序didFinishLaunchingWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 

    //Check if rootViewController is TabBar 
    if (window?.rootViewController as? UITabBarController) != nil { 

     //Change unselected TintColor 
     (window?.rootViewController as! UITabBarController).tabBar.tintColor = UIColor(red: 255/255, green: 102/255, blue: 0, alpha: 1.0) 

     //If system has IOS 10 or newer 
     if #available(iOS 10.0, *) { 
      //Change Unselected Tint Color 
      (window?.rootViewController as! UITabBarController).tabBar.unselectedItemTintColor = UIColor.black 
     } else { 
      // Fallback on earlier versions 
     } 

    } 

    return true 
}