2010-06-16 107 views

回答

0

查看this questionthis question的回答,但請注意,您的應用可能因爲修改默認的tabbar組件而被拒絕。

+0

非常感謝您的快速響應,但這並不能幫助我,因爲我可以更改圖像顏色,並且此代碼也是如此。請向我建議如何更改uitabbaritem的文本顏色。 – yogendra 2010-06-16 09:25:22

+0

這個問題正在改變標籤欄項目上的圖像,我需要更改標籤baritem上的文本顏色 – 2012-06-08 08:30:27

1

UITabBarItem是相當多的非定製的,所以如果你一定要,你可以:

  1. 肩扛通過迭代通的UITabBar的子視圖,使用-[NSObject isKindOfClass:]找到標籤和改變自己的顏色。

  2. 創建自己的UITabBar並滾動自定義選項卡欄項目。

  3. 嘗試類似Three20的替代方案TTTabBar

+0

哪些obj應該在子視圖內尋找?我登錄,我看到有一堆'UITabBarButton',但不知道如何使用該類!? – 2011-05-18 22:10:17

+0

我最終創建了記錄的自定義標籤欄視圖。 – 2011-05-19 08:19:12

2

編輯:下面是不再最佳做法,因爲新的API被添加到了iOS SDK

子類的UITabBarController(如CustomTabBarController在這個例子中),並把下面的代碼在您的m實現文件:

@interface CustomTabBarController() 

@property (nonatomic, retain) NSArray *tabTitleLabels; 

@end 


@implementation CustomTabBarController 

@synthesize tabTitleLabels; 

- (NSArray *)tabTitleLabels 
{ 
    // Check if we need to update the tab labels 
    if ([tabTitleLabels count] != [self.viewControllers count]) 
     self.tabTitleLabels = nil; 

    // Create custom tab bar title labels 
    if (!tabTitleLabels) 
    { 
     tabTitleLabels = [[NSMutableArray alloc] init]; 

     for (UIView *view in self.tabBar.subviews) 
     {  
      if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) 
      { 
       for (UIView *subview in view.subviews) 
       {          
        if ([subview isKindOfClass:[UILabel class]]) 
        { 
         UILabel *label = (UILabel *)subview; 

         UILabel *newLabel = [[UILabel alloc] init]; 
         newLabel.font = label.font; 
         newLabel.text = label.text; 
         newLabel.backgroundColor = label.backgroundColor; 
         newLabel.opaque = YES; 
         newLabel.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.height -1);  
         [subview addSubview:newLabel]; 

         [((NSMutableArray *)tabTitleLabels) addObject:newLabel]; 
         [newLabel release]; 
        } 
       } 
      } 
     }  
    } 

    return tabTitleLabels; 
} 

// Customize the desired colors here 
- (void)recolorTabBarTitleLabels 
{ 
    for (UILabel *label in self.tabTitleLabels) 
    { 
     label.textColor = [UIColor whiteColor]; 
     label.backgroundColor = [UIColor blackColor]; 
    } 
    UILabel *selectedLabel = [self.tabTitleLabels objectAtIndex:self.selectedIndex]; 
    selectedLabel.textColor = [UIColor blueColor];    
    selectedLabel.backgroundColor = [UIColor colorWithWhite:.15 alpha:1]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self recolorTabBarTitleLabels]; 
} 

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController 
{ 
    [self recolorTabBarTitleLabels]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    self.tabTitleLabels = nil; 
} 

- (void)dealloc 
{ 
    [tabTitleLabels release]; 
    [super dealloc]; 
} 

@end 

這可能會晚一年,但我希望我的代碼能夠爲某些人省點工作!

注意:它不支持切換進/出新的標籤欄項目,儘管您只需將tabTitleLabels重置爲零即可。

74

老問題,但我有

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } 
             forState:UIControlStateNormal]; 
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } 
             forState:UIControlStateSelected]; 
+0

+用新的最佳做法進行更新。 – DetartrateD 2012-09-24 17:48:09

+0

同意Detartrate的評論,因爲你什麼時候可以使用@ {} ...創建一個字典Awesomeness。非常JSONesque。 – 2013-04-16 18:57:01

+0

由於LLVM 4.0編譯器。查看文檔以查看可以使用文字處理的所有其他內容:http://clang.llvm.org/docs/ObjectiveCLiterals.html。您還可以將當前項目轉換爲新的字面語法:XCode> Edit> Refactor>轉換爲現代Objective-C語法....現在已經存在了大約一年,現在我會說。 – bandejapaisa 2013-04-16 21:22:51

11

UITextAttributeTextColor從iOS版7.使用NSForegroundColorAttributeName不提倡使用的是iOS 5中支持起(也我使用LLVM 4.0文本)的新答案代替。

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] } 
              forState:UIControlStateNormal]; 

而且在斯威夫特

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: .Normal) 
0

設置顏色爲2 UIControlState一次可以使用union

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected.union(UIControlState.Highlighted)) 
2

它可以幫助你

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

Swift3

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal) 
0

保持簡單!

[[UITabBar appearance] setTintColor:[UIColor blackColor]]; 
+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18808729) – 2018-02-13 17:01:37

+0

歡迎來到StackOverflow。只有代碼在他們的答案往往會被標記爲刪除,因爲他們是「低質量」。請閱讀關於回答問題的幫助部分,然後考慮在答案中添加一些評論。 – Graham 2018-02-14 02:05:19

0

由於iOS的10也能夠設置unselectedItemTintColorUITabBar

UITabBartintColor是比selectedItem的顏色。

如果你想去唯一值的任何項目,您還可以直接與tabBarItem.imagetabBarItem.selectedImage設置tabBarItem.titleTextAttributes(for:)(前面提到的)也對項目組合。