[英]TabBar item tint color does not change
問題在於您無法控制未選中項目的色澤。 這不是代碼的錯; 這就是iOS的工作方式。 這曾經是可能的,但在某些時候(iOS 7?不記得了),它就消失了。
因此,屏幕截圖中發生的事情是,您將選定的色調顏色設置為橙色,到此為止。 一個選項卡欄項目被選中,並被着色為橙色。
解決方案之一是提供兩組選項卡圖標。 有一篇帖子與您的情況非常相似,您可以看一下: 自定義標簽欄圖標顏色
我認為這段代碼(由Tunvir Rahman 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
如果您是為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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.