[英]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.