[英]swift UITabbaritem colors
我試圖弄清楚如何為我的tabBar
使用我想要的顏色。
我知道如何更改背景,我也知道如何更改tabbar.item
顏色和文本,但我不明白如何:
如果項目被選中,則更改顏色(我使用的渲染模式始終是原始的,因為我找不到另一種方法來從非選定的選項卡欄項目中刪除默認的灰色)
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) tabBarItem.title = "test" tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) }
我怎樣才能在我想要的狀態下使用我想要的顏色?
UITabBar
有一個tintColor
屬性,但它設置了所選圖像的色調,而不是未選擇的圖像。 您正在正確設置未選擇的圖像 AFAIK。 要更改所選圖像的顏色,您可以使用UITabBar
上的tintColor
(如果您希望所有圖像具有相同的色調),或者將您的UITabBarItem
的selectedImage
的渲染模式設置為AlwaysOriginal
。
tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)
我已將 UIImage 設置為未包裝的可選,因為如果沒有圖像文件,您可能希望它崩潰。 這將有助於確保您的圖像確實被加載,而不是默默地失敗:-)
您可能還想設置標簽的顏色,否則您的文本將與您的圖像顏色不匹配。 下面為所有UITabBarItem
設置默認值,但您可以在每個項目的基礎上設置(或覆蓋)它。
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal)
這是你如何快速 3 / 4
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .normal)
如果你想:
tabBarItem
來顯示彩色圖像tabBarItem
以顯示灰色圖像然后你需要確保 XCode 中的相關圖片資源設置為Render as: Default
,然后:
let image = SomeImage
tabBarItem.image = image
tabBarItem.selectedImage = image.withRenderingMode(.alwaysOriginal)
這確保對於selectedImage
情況,您強制圖像顯示為原始圖像,並且在任何其他情況下,它將使用預期的色調進行渲染。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.