xcode swift UITabbaritem 颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26704896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift UITabbaritem colors
提问by Marianna
I'm trying to figure out how to use the colors I want for my tabBar
.
我试图弄清楚如何使用我想要的颜色tabBar
。
I know how to change the background, I also know how to change the tabbar.item
colors and text but I can't understand how to:
我知道如何更改背景,我也知道如何更改tabbar.item
颜色和文本,但我不明白如何:
- the default gray color of the unselected tab bar item
to change the color if the item is selected (and I'm using rendering mode always original cause I can't find another way to remove the default gray color from the non selected tab bar 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) }
- 未选中的标签栏项目的默认灰色
如果项目被选中,则更改颜色(我使用的渲染模式总是原始的,因为我找不到另一种方法来从非选定的标签栏项目中删除默认的灰色)
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) }
how can I use the color I want, in the state I want?
我怎样才能在我想要的状态下使用我想要的颜色?
回答by rickerbh
A UITabBar
has a tintColor
property, but this sets the tint for the selected image, not the unselected one. You're setting the unselected image correctly AFAIK. For changing the colour of the selected image, you can use the tintColor
on the UITabBar
(if you want all the images to have the same tint), or set your UITabBarItem
's selectedImage
with the rendering mode as AlwaysOriginal
.
AUITabBar
有一个tintColor
属性,但这会为所选图像设置色调,而不是未选择的图像。您正在正确设置未选择的图像 AFAIK。为了改变所选图像的颜色,你可以使用tintColor
的UITabBar
(如果你希望所有的图像具有相同的色调),或将您UITabBarItem
的selectedImage
使用渲染模式AlwaysOriginal
。
tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)
I've set the UIImage to be an unwrapped optional because you probably want it to crash if there is no image file. It'll help ensure your images are actually being loaded, rather than silently failing :-)
我已将 UIImage 设置为未包装的可选,因为如果没有图像文件,您可能希望它崩溃。这将有助于确保您的图像确实被加载,而不是默默地失败:-)
You may also want to set the colour for the label or your text won't match your image colours. The below sets the defaults for all UITabBarItem
's, but you can set (or override) it on a per item basis.
您可能还想设置标签的颜色,否则您的文本将与您的图像颜色不匹配。下面为 all 设置默认值UITabBarItem
,但您可以在每个项目的基础上设置(或覆盖)它。
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal)
回答by John Riselvato
Here's how you do it in swift 3 / 4
这是您如何快速 3 / 4
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .normal)
回答by justsee
If you want:
如果你想:
- a selected
tabBarItem
to display a colour image - an unselected
tabBarItem
to display a greyed-out image
- a 选择
tabBarItem
显示彩色图像 - 未选中
tabBarItem
以显示灰色图像
Then you need to ensure the relevant image assets in XCode are set as Render as: Default
, then:
然后需要确保XCode中的相关图片资源设置为Render as: Default
,那么:
let image = SomeImage
tabBarItem.image = image
tabBarItem.selectedImage = image.withRenderingMode(.alwaysOriginal)
This ensures that for the selectedImage
case you are forcing the image to display as the original, and in any other situation it will render with the expected tints applied.
这确保在selectedImage
您强制图像显示为原始图像的情况下,在任何其他情况下,它将使用预期的色调进行渲染。