在选定的选项卡栏项目上设置文本颜色

发布于 2024-10-05 19:46:13 字数 78 浏览 8 评论 0原文

我创建了一个标签栏应用程序。我有四个选项卡;在选定的选项卡上,我需要将选项卡标题设置为红色。我怎样才能做到这一点?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

真心难拥有 2024-10-12 19:46:14

这是快速版本:-

    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)

    }

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)

    }
夏花。依旧 2024-10-12 19:46:14

如果我正确理解您的问题,您想自定义 UITabBarItem 上文本的颜色。不幸的是,它确实没有那么灵活。如果您打算这样做(除非您在专业人士的帮助下仔细考虑了设计,否则我建议您不要这样做!),您将不得不做一些非常可怕的事情才能使其发挥作用。

我建议迭代 UITabBar 的子视图(根据需要遍历多个级别),并查找 UILabel 对象。如果找到一些,您可以更改它们的颜色。如果不这样做,则意味着它的实现方式不同(可能在某处的 -drawRect: 方法中);如果发生这种情况,你真的应该放弃。

无论您决定做什么,祝您好运。

If I understand your question correctly, you want to customize the color of the text on the UITabBarItems. 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. 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); if this happens to be the case, you really ought to give up.

Best of luck on whatever you decide to do.

最美不过初阳 2024-10-12 19:46:14

这可以通过 -drawRect: 实现,但这样做会大大增加您的应用被 App Store 拒绝的机会

That is possible by -drawRect:, but by doing that you are highly increasing chances of your app being rejected by App Store

浮云落日 2024-10-12 19:46:13

使用 UIAppearance 协议 (iOS5+) 现在可以实现这一点,而且实际上非常简单。

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

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

请原谅糟糕的颜色!

Using the UIAppearance protocol (iOS5+) this is now possible, and actually pretty easy.

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

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

Please excuse the awful colors!

紫轩蝶泪 2024-10-12 19:46:13

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

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

Anyone who's looking for Swift 4 solution..

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: .red], for: .selected)
柠栀 2024-10-12 19:46:13

这最终对我有用:

1)选定的文本颜色

[[UIView appearance] setTintColor:someColor];

2)未选定的文本(也改变图像颜色)

[[UITabBar appearance] setTintColor:anotherColor];

This is what finally worked for me:

1)Selected text color

[[UIView appearance] setTintColor:someColor];

2)Unselected text(also changes image color)

[[UITabBar appearance] setTintColor:anotherColor];
风铃鹿 2024-10-12 19:46:13

只是为了澄清一下......

如果您想更改所有选项卡栏项目的外观,请使用:

Objective-C

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

Swift

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

但是< /strong>,如果您只想设置单个项目的外观,请像这样操作:

Objective-C

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

Swift

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

注意tabBarItemUIViewController 上的一个属性。这意味着虽然每个 UIViewController 都有此属性,但它可能不是您要查找的 tabBarItem。当您的视图控制器包含在 UINavigationController 中时,通常会出现这种情况。在这种情况下,访问导航控制器上的 tabBarItem 而不是其根(或其他)视图控制器中的。

Just to clear things up a bit…

If you'd like to change the appearance for all tab bar items, use:

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:

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

Swift:

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

Note: tabBarItem is a property on UIViewController. This means that while every UIViewController has this property, it may not be the tabBarItem you're looking for. This is often the case when your view controller is enclosed in a UINavigationController. In this instance, access the tabBarItem on the navigation controller not the one in it's root (or other) view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文