簡體   English   中英

TabBar項目色彩顏色不變

[英]TabBar item tint color does not change

我有一個帶有5個標簽的標簽欄。 我為tab bar items selectedunselected狀態放置了不同的圖像。

不管我做什么,色調顏色都不會改變,也不會適應圖像顏色。

選擇選項卡時,顏色應為黑色,而未選擇時,顏色應為橙色。

這是分配了圖像的屬性檢查器的圖像。

在此處輸入圖片說明

標簽欄的圖像

在此處輸入圖片說明

如何更改圖像顏色?

問題在於您無法控制未選中項目的色澤。 這不是代碼的錯; 這就是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.

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