任何人都可以幫助我在這個奇怪的問題是有反正uitabbar項目的文本顏色從默認灰色更改爲白色和選定的顏色藍色。改變uitabbaritem的文字顏色
回答
查看this question和this question的回答,但請注意,您的應用可能因爲修改默認的tabbar組件而被拒絕。
UITabBarItem是相當多的非定製的,所以如果你一定要,你可以:
肩扛通過迭代通的
UITabBar
的子視圖,使用-[NSObject isKindOfClass:]
找到標籤和改變自己的顏色。創建自己的
UITabBar
並滾動自定義選項卡欄項目。嘗試類似Three20的替代方案
TTTabBar
。
哪些obj應該在子視圖內尋找?我登錄,我看到有一堆'UITabBarButton',但不知道如何使用該類!? – 2011-05-18 22:10:17
我最終創建了記錄的自定義標籤欄視圖。 – 2011-05-19 08:19:12
編輯:下面是不再最佳做法,因爲新的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重置爲零即可。
老問題,但我有
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] }
forState:UIControlStateSelected];
+用新的最佳做法進行更新。 – DetartrateD 2012-09-24 17:48:09
同意Detartrate的評論,因爲你什麼時候可以使用@ {} ...創建一個字典Awesomeness。非常JSONesque。 – 2013-04-16 18:57:01
由於LLVM 4.0編譯器。查看文檔以查看可以使用文字處理的所有其他內容:http://clang.llvm.org/docs/ObjectiveCLiterals.html。您還可以將當前項目轉換爲新的字面語法:XCode> Edit> Refactor>轉換爲現代Objective-C語法....現在已經存在了大約一年,現在我會說。 – bandejapaisa 2013-04-16 21:22:51
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)
設置顏色爲2 UIControlState
一次可以使用union
:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected.union(UIControlState.Highlighted))
它可以幫助你
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
Swift3
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal)
保持簡單!
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
這不提供問題的答案。一旦你有足夠的[聲譽](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-can- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18808729) – 2018-02-13 17:01:37
歡迎來到StackOverflow。只有代碼在他們的答案往往會被標記爲刪除,因爲他們是「低質量」。請閱讀關於回答問題的幫助部分,然後考慮在答案中添加一些評論。 – Graham 2018-02-14 02:05:19
由於iOS的10也能夠設置unselectedItemTintColor
上UITabBar
。
UITabBar
的tintColor
是比selectedItem的顏色。
如果你想去唯一值的任何項目,您還可以直接與tabBarItem.image
和tabBarItem.selectedImage
設置tabBarItem.titleTextAttributes(for:)
(前面提到的)也對項目組合。
- 1. 如何改變顏色的文字在UITabBarItem在iOS 4的
- 2. swift UITabbaritem顏色
- 3. 更改Swift中UITabBarItem的背景顏色
- 4. 改變顏色的顏色
- 5. 文字RecyclerView TextView的顏色改變
- 6. 更改文字的顏色,無邊框顏色變化
- 7. 如何修改UITabBarItem中文本的顏色?
- 8. 點擊jquery改變文字顏色
- 9. 狀態欄文字顏色不改變
- 10. 隨着時間改變文字顏色
- 11. Javascript功能改變文字顏色
- 12. 初始屏幕文字改變顏色
- 13. 如何在uitabbarcontroller中更改默認的uitabbaritem的灰色顏色?
- 14. 如何改變文字顏色股利不改變邊框顏色
- 15. TextView改變背景色的文字顏色變化
- 16. 更改UITabBarItem未選中的顏色色調 - Swift
- 17. 在iOS 10中更改UITabBarItem的選定色調顏色
- 18. 顏色改變文本
- 19. 改變顏色
- 20. 改變顏色
- 21. 改變顏色
- 22. 改變顏色
- 23. 改變顏色
- 24. 改變顏色
- 25. 改變顏色
- 26. 改變顏色
- 27. 改變顏色
- 28. 如何選擇更改文本顏色UITabBarItem?
- 29. 改變原子的顏色的顏色
- 30. jquery - 更改文字顏色
非常感謝您的快速響應,但這並不能幫助我,因爲我可以更改圖像顏色,並且此代碼也是如此。請向我建議如何更改uitabbaritem的文本顏色。 – yogendra 2010-06-16 09:25:22
這個問題正在改變標籤欄項目上的圖像,我需要更改標籤baritem上的文本顏色 – 2012-06-08 08:30:27