在选定的选项卡栏项目上设置文本颜色
我创建了一个标签栏应用程序。我有四个选项卡;在选定的选项卡上,我需要将选项卡标题设置为红色。我怎样才能做到这一点?
提前致谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是快速版本:-
This is the swift version :-
如果我正确理解您的问题,您想自定义
UITabBarItem
上文本的颜色。不幸的是,它确实没有那么灵活。如果您打算这样做(除非您在专业人士的帮助下仔细考虑了设计,否则我建议您不要这样做!),您将不得不做一些非常可怕的事情才能使其发挥作用。我建议迭代
UITabBar
的子视图(根据需要遍历多个级别),并查找UILabel
对象。如果找到一些,您可以更改它们的颜色。如果不这样做,则意味着它的实现方式不同(可能在某处的-drawRect:
方法中);如果发生这种情况,你真的应该放弃。无论您决定做什么,祝您好运。
If I understand your question correctly, you want to customize the color of the text on the
UITabBarItem
s. 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 forUILabel
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.
这可以通过
-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使用 UIAppearance 协议 (iOS5+) 现在可以实现这一点,而且实际上非常简单。
请原谅糟糕的颜色!
Using the UIAppearance protocol (iOS5+) this is now possible, and actually pretty easy.
Please excuse the awful colors!
任何正在寻找 Swift 4 解决方案的人..
Anyone who's looking for Swift 4 solution..
这最终对我有用:
1)选定的文本颜色
2)未选定的文本(也改变图像颜色)
This is what finally worked for me:
1)Selected text color
2)Unselected text(also changes image color)
只是为了澄清一下......
如果您想更改所有选项卡栏项目的外观,请使用:
Objective-C:
Swift:
但是< /strong>,如果您只想设置单个项目的外观,请像这样操作:
Objective-C:
Swift:
注意:
tabBarItem
是UIViewController
上的一个属性。这意味着虽然每个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:
Swift:
However, if you just want to set the appearance of a single item do it like so:
Objective-C:
Swift:
Note:
tabBarItem
is a property onUIViewController
. This means that while everyUIViewController
has this property, it may not be thetabBarItem
you're looking for. This is often the case when your view controller is enclosed in aUINavigationController
. In this instance, access thetabBarItem
on the navigation controller not the one in it's root (or other) view controller.