【发布时间】:2015-10-16 19:20:37
【问题描述】:
我有一个带有 5 个标签的标签栏。我为selected 和unselected 的tab bar items 状态放置了不同的图像。
无论我做什么,色调都不会改变,也不会适应图像颜色。
当标签被选中时,颜色应该是黑色,当没有选中时,它应该是橙色。
这是分配了图像的属性检查器的图像。
标签栏的图片
如何更改图像颜色?
【问题讨论】:
标签: ios objective-c uitabbaritem
我有一个带有 5 个标签的标签栏。我为selected 和unselected 的tab bar items 状态放置了不同的图像。
无论我做什么,色调都不会改变,也不会适应图像颜色。
当标签被选中时,颜色应该是黑色,当没有选中时,它应该是橙色。
这是分配了图像的属性检查器的图像。
标签栏的图片
如何更改图像颜色?
【问题讨论】:
标签: ios objective-c uitabbaritem
问题是您无法控制未选中项目的色调颜色。这不是你的代码的错;这就是 iOS 的工作方式。这使用是可能的,但在某些时候(iOS 7?不记得)它就消失了。
所以在您的屏幕截图中发生的情况是,您已将选定的色调设置为橙色,这就是结束。一个标签栏项目被选中,它被染成橙色。
【讨论】:
image 和非着色selectedImage,现在将直接使用这些图像的颜色(和透明度)。
tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray 在您的应用委托中
其中一个解决方案是提供两组选项卡图标。有一个帖子和你的情况很相似,你可以看一下: Custom tab bar icon colors
我认为这段代码(由 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 或更高版本开发,您可以更改未选择的 tint 颜色,在旧版本中您只能更改选定的 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
}
【讨论】: