ios 如何更改 UITabBar 选择颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/790746/
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
How to change UITabBar Selection color
提问by saikamesh
I need to change the selection color of UITabBar from default blue to red. How do we do this.
我需要将 UITabBar 的选择颜色从默认的蓝色更改为红色。我们如何做到这一点。
回答by Behdad
Update September 2017:It's been two years since I've written this answer and since it's receiving upvotes regularly, I should say this is probably the worst possible answer to this question, it's error prone, likely to break because of iOS updates, hard to debug, etc., so please don't do the things I've written and apply better solutions such as subclassing UITabBar or UITabBarController. Thanks.
2017 年 9 月更新:我写这个答案已经两年了,因为它定期收到投票,我应该说这可能是这个问题最糟糕的答案,它容易出错,可能会因 iOS 更新而中断,很难调试等,所以请不要做我写的事情并应用更好的解决方案,例如子类化 UITabBar 或 UITabBarController。谢谢。
You can do this by setting a "tintColor" attribute (Key Path) for you UITabBar.
您可以通过为您的 UITabBar 设置“tintColor”属性(键路径)来做到这一点。
- Select the UITabBar in the document outline. (NOTthe Controller with the yellow icon.)
- Select Identity Inspector in the Utilities area.
- Click the + in "User Defined Runtime Attributes."
- Add a "tintColor" Key Path of type "Color" and the color you want.
- 在文档大纲中选择 UITabBar。(不是带有黄色图标的控制器。)
- 在 Utilities 区域中选择 Identity Inspector。
- 单击“用户定义的运行时属性”中的 +。
- 添加“颜色”类型的“tintColor”键路径和您想要的颜色。
This should do it. You can check it against the screenshot below.
这应该这样做。您可以对照下面的屏幕截图进行检查。
More on this:There's a "Tint" attribute in Identity Inspector of UITabBar which I believed would do the exact same thing but apparently, it does nothing. It's default value is the exact default fill color when a UITabBarItem is selected, so my guess is it would be fixed in the stable release Xcode 7. Fingers crossed.
更多相关信息:UITabBar 的 Identity Inspector 中有一个“Tint”属性,我相信它会做完全相同的事情,但显然它什么都不做。它的默认值是选择 UITabBarItem 时的确切默认填充颜色,所以我猜它会在稳定版 Xcode 7 中修复。手指交叉。
回答by Daniele Grassi
In IOS5, UITabBar has a selectedImageTintColorproperty which does what you need.
在 IOS5 中, UITabBar 有一个selectedImageTintColor属性,它可以满足您的需求。
回答by Kyle Clegg
In iOS 7 it's simply the tintColor. One way to accomplish this could be to subclass UITabBarViewController, set the custom class in the storyboard, and in your viewDidLoad
method of the subclassed tabBarVC add this:
在 iOS 7 中,它只是 tintColor。实现此目的的一种方法可能是将 UITabBarViewController 子类化,在故事板中设置自定义类,并在viewDidLoad
子类化的 tabBarVC 的方法中添加以下内容:
[[self tabBar] setTintColor:[UIColor redColor]];
回答by Daniel
It is extremely easy
这非常容易
Create a custom class of UITabBarControllerand in -(void)viewDidLoad
method add this line:
创建一个自定义的UITabBarController类并在-(void)viewDidLoad
方法中添加以下行:
[[self tabBar] setSelectedImageTintColor:[UIColor greenColor]];
回答by Zaid Pathan
To achieve above result perform following steps.
要达到上述结果,请执行以下步骤。
Step 1:Add your desired images in Assets.xcassets
, and make sure they Render As
: Default
第 1 步:在 中添加您想要的图像Assets.xcassets
,并确保它们Render As
:Default
Step 2:Select your UITabBar
object and set Image Tint
color, this color will be selected tab color
第 2 步:选择您的UITabBar
对象并设置Image Tint
颜色,此颜色将被选择为标签颜色
Step 3:Select UITabBar
object and add Key Path: unselectedItemTintColor
, Type: Color
, Value: Choose color for unselected item
in User Defined Runtime Attributes.
步骤 3:选择UITabBar
对象并添加Key Path: unselectedItemTintColor
, Type: Color
, Value: Choose color for unselected item
in User Defined Runtime Attributes。
All done.
全部完成。
回答by Gabriel.Massana
Because UITextAttributeTextColor is deprecated in iOS 7, you should use:
因为 UITextAttributeTextColor 在 iOS 7 中被弃用,你应该使用:
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
回答by Vrutin Rathod
Simply change the following property in Interface Builder for the TabBar
只需在界面生成器中为 TabBar 更改以下属性
Obviously in my case its White.
显然,就我而言,它是白色的。
回答by amattn
The SDK does not make this easy, but it is technicallypossible. Apple apparently believes this to be part of their vision of a consistent look and feel.
SDK 并没有让这一切变得容易,但在技术上是可行的。苹果显然认为这是他们对一致外观和感觉的愿景的一部分。
UITabBar is a subclass of UIView. You can always subclass and implement your own -drawRect:
UITabBar 是 UIView 的子类。你总是可以子类化并实现你自己的-drawRect:
This is not a trivial task, however, you have to essentially re-implement the class from scratch or you risk some weird side-effects.
这不是一项微不足道的任务,但是,您必须从头开始重新实现该类,否则您将面临一些奇怪的副作用。
回答by Ben Clayton
I've been searching for a way to set the selected text color of a UITabBarItem and have found a dead simple method, using the UIAppearance protocol.
我一直在寻找一种方法来设置 UITabBarItem 的选定文本颜色,并找到了一种使用 UIAppearance 协议的简单方法。
[UITabBarItem.appearance setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected];
Please excuse the awful colors!
请原谅糟糕的颜色!
回答by Andrey Gordeev
Starting from iOS 8 it's as simple as:
从 iOS 8 开始,它很简单:
UITabBar.appearance().tintColor = UIColor.redColor()