[英]How to change tab bar item text color
如何更改标签栏中“更多..”文本的颜色以匹配其图标颜色。 (现在在标签栏中选择了性能)
我试图设置 TitleTextAttributes。
[moreItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName, [UIColor yellowColor],NSForegroundColorAttributeName , nil]
但它的文本颜色始终设置为黄色。 即使选择了该项目。 像这样
我试图在选择时设置为白色,而在未选择时它应该与图标颜色匹配。 谢谢..任何建议都会非常有帮助。
接受的答案的代码对我不起作用。
这是代码,有效:
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor yellowColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateSelected];
我找到了我自己问题的答案。
我们可以为两种不同的状态设置perforamceItem setTitleTextAttributes:
forState:UIControlStateNormal
forState:UIControlStateHighlighted
我添加了以下代码
[performanceItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName, [UIColor yellowColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
[performanceItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateHighlighted];
我需要用我的图标的颜色替换黄色。 这就是他们现在的样子。
斯威夫特 5.1 + iOS 12.4 和 iOS 13 :
/// Subclass of `UITabBarController` that is used to set certain text colors for tab bar items.
class TabBarController: UITabBarController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let items = tabBar.items {
// Setting the title text color of all tab bar items:
for item in items {
item.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
item.setTitleTextAttributes([.foregroundColor: UIColor.lightGray], for: .normal)
}
}
}
}
这是快速版本:-
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)
}
或者您可以简单地更改 Appdelegate :-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: .Normal)
// Override point for customization after application launch.
return true
}
斯威夫特 4:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], for: .selected)
对于快速解决方案,让类型推断成为您的朋友:
override func viewWillAppear(animated: Bool) {
for item in self.tabBar.items! {
let unselectedItem = [NSForegroundColorAttributeName: UIColor.blackColor()]
let selectedItem = [NSForegroundColorAttributeName: UIColor.whiteColor()]
item.setTitleTextAttributes(unselectedItem, forState: .Normal)
item.setTitleTextAttributes(selectedItem, forState: .Selected)
}
}
@skywinder 的 Swift 版本回答:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
这很简单,只需将 UITabBarItem 子类化并将其分配为故事板或代码中标签栏项目的类。 以下非常适合我。
import UIKit
class PPTabBarItem: UITabBarItem {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init() {
super.init()
commonInit()
}
func commonInit() {
self.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13), NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Normal)
self.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13), NSForegroundColorAttributeName:UIColor.yellowColor()], forState: UIControlState.Selected)
}
}
skywinder 的解决方案很好,但它触发了全局范围。
这工作正常..
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
这在 Swift 5 上对我有用。
在 AppDelegate 中:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], for: .selected)
现在,如果您的应用支持低于 13 的 iOS 版本,您应该以不同的方式设置此颜色:
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: .red]
tabBar.standardAppearance = appearance
} else {
UITabBarItem.appearance().setTitleTextAttributes(UIColor.red, for: UIControl.State.selected)
}
在代码示例中,我为 UITabBarItem 的选定状态设置了红色文本颜色,您可能还需要更改正常状态的文本颜色。
你可以设置UITabBar的tintcolor和unselectedItemTintColor来改变它的外观。
self.tabBar.tintColor = // Your colour
self.tabBar.unselectedItemTintColor = // Your unselected item colour
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.