2010-12-04 86 views

回答

0

如果我正確理解您的問題,您想要自定義UITabBarItem上的文字顏色。不幸的是,它確實不那麼靈活。如果你打算這樣做(除非你已經在專業人員的幫助下仔細考慮了設計,否則我不推薦!),你必須做一些非常可怕的事情才能實現它。

我建議遍歷UITabBar的子視圖(儘可能多的級別),並尋找UILabel對象。如果你發現一些,你可以改變它們的顏色。如果你不這樣做,這意味着它的實現方式不同(可能在某個地方的-drawRect:方法中);如果發生這種情況,你真的應該放棄。

祝你好運,無論你決定做什麼。

+0

@喬納森英鎊......感謝花花公子......我會嘗試 – 2010-12-04 09:51:07

+0

沒問題! :-) – 2010-12-04 09:57:03

-1

也就是說可以通過-drawRect:,但這樣做你的應用程序中,你是高度增加機會通過App Store的

+0

什麼? Drawrect是佈局UIView的標準方法。 – quantumpotato 2014-08-08 16:02:42

22

被拒絕使用UIAppearance協議(的iOS5 +),這是目前可能的,而且實際上是相當容易的。

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

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

請原諒可怕的顏色!

1

這是最終爲我工作:

1)選定的文本顏色

[[UIView appearance] setTintColor:someColor]; 

2)未選擇的文本(也改變圖像色彩)

[[UITabBar appearance] setTintColor:anotherColor]; 
2

只是爲了澄清事實有點...

如果您想要更改所有標籤欄項目的外觀,請使用:

Objective-C的

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

斯威夫特

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

然而,如果你只是想設置單個項目的外觀做它像這樣:

Objective-C

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

斯威夫特

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

注意tabBarItemUIViewController的屬性。這意味着雖然每個UIViewController都有此屬性,但它可能不是您要查找的tabBarItem。當您的視圖控制器包含在UINavigationController中時,通常會出現這種情況。在這種情況下,請訪問導航控制器上的tabBarItem而不是其根目錄(或其他)視圖控制器中的那個。

1

這是SWIFT版本: -

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) 

    }