简体   繁体   English

在所选标签栏项目上设置文本颜色

[英]Setting text-color on selected tab bar item

I've created a tab bar application. 我已经创建了一个标签栏应用程序。 I have four tabs; 我有四个标签; on the selected tab, I need to set the color red for tab title. 在选定的选项卡上,我需要为选项卡标题设置红色。 How can i do that? 我怎样才能做到这一点?

Thanks in advance. 提前致谢。

Using the UIAppearance protocol (iOS5+) this is now possible, and actually pretty easy. 使用UIAppearance协议(iOS5 +)现在可以实现,实际上非常简单。

[UITabBarItem.appearance setTitleTextAttributes:@{
        UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];

[UITabBarItem.appearance setTitleTextAttributes:@{
        UITextAttributeTextColor : [UIColor purpleColor] }     forState:UIControlStateSelected];

Please excuse the awful colors! 请原谅可怕的颜色!

Just to clear things up a bit… 只是为了清理一下......

If you'd like to change the appearance for all tab bar items, use: 如果您想更改所有标签栏项目的外观,请使用:

Objective-C : Objective-C

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected];

Swift : 斯威夫特

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected)

However , if you just want to set the appearance of a single item do it like so: 但是 ,如果您只想设置单个项目的外观,请执行以下操作:

Objective-C : Objective-C

[self.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected];

Swift : 斯威夫特

tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected)

Note : tabBarItem is a property on UIViewController . 注意tabBarItemUIViewController上的一个属性。 This means that while every UIViewController has this property, it may not be the tabBarItem you're looking for. 这意味着虽然每个UIViewController都有此属性,但它可能不是您正在寻找的tabBarItem This is often the case when your view controller is enclosed in a UINavigationController . 当您的视图控制器包含在UINavigationController时,通常会出现这种情况。 In this instance, access the tabBarItem on the navigation controller not the one in it's root (or other) view controller. 在这种情况下,访问导航控制器上的tabBarItem而不是其根(或其他)视图控制器中的tabBarItem

This is what finally worked for me: 这最终对我有用:

1)Selected text color 1)选定的文字颜色

[[UIView appearance] setTintColor:someColor];

2)Unselected text(also changes image color) 2)未选择的文本(也改变图像颜色)

[[UITabBar appearance] setTintColor:anotherColor];

This is the swift version :- 这是迅捷版: -

    for item in self.mainTabBar.items! {

    let unselectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    let selectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    item.setTitleTextAttributes(unselectedItem as? [String : AnyObject], forState: .Normal)
    item.setTitleTextAttributes(selectedItem as? [String : AnyObject], forState: .Selected)

    }

任何正在寻找Swift 4解决方案的人

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: .red], for: .selected)

If I understand your question correctly, you want to customize the color of the text on the UITabBarItem s. 如果我正确理解您的问题,您需要自定义UITabBarItem上文本的颜色。 Unfortunately, it's really not that flexible. 不幸的是,它真的不那么灵活。 If you're intent on doing this (which, unless you've carefully considered the design with the help of a professional, I recommend against!), you'll have to do some really frightening things to get this to work. 如果你打算这样做(除非你在专业人士的帮助下仔细考虑过这个设计,我建议反对!),你必须做一些非常可怕的事情才能让它发挥作用。

I'd suggest iterating through the subviews of the UITabBar (as many levels as necessary), and looking for UILabel objects. 我建议迭代UITabBar的子视图(根据需要多个级别),并寻找UILabel对象。 If you find some, you can change their color. 如果你找到一些,你可以改变它们的颜色。 If you don't, that means that it is implemented differently (probably in a -drawRect: method somewhere); 如果不这样做,这意味着它的实现方式不同(可能在-drawRect:方法中); if this happens to be the case, you really ought to give up. 如果碰巧是这种情况,你真的应该放弃。

Best of luck on whatever you decide to do. 无论你决定做什么,祝你好运。

这可以通过-drawRect: ,但通过这样做,您的应用程序被App Store拒绝的可能性大大增加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM