![](/img/trans.png)
[英]How can I change a UITabBarItem title to the title from another UITabBarItem?
[英]How can I change color of UITabBarItem's title when it's selecetd
我的環,
iOS6
Xcode 4.5.1
我想在選中時更改UITabBarItem標題的顏色。
我為UITabBarItem使用CustomUITabBarItem。 - customUITabBarItem.m
@implementation customUITabBarItem
@synthesize customHighlightedImage;
-(UIImage *) selectedImage
{
return self.customHighlightedImage;
}
- (void) dealloc
{
[customHighlightedImage release];
customHighlightedImage=nil;
[super dealloc];
}
@end
ViewController.m
#import "FirstViewController.h"
#import "customUITabBarItem.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CustomUITabBarItem *tabItem = [[customUITabBarItem alloc] initWithTitle:@"first" image:[UIImage imageNamed:@"first.png"] tag:0];
tabItem.customHighlightedImage = [UIImage imageNamed:@"first_selected.png"];
self.tabBarItem = tabItem;
[tabItem release];
tabItem = nil;
}
我怎樣才能改變顏色?
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
[UIColor grayColor],
nil]];
請確保這僅適用於iOS 5.0或更高版本。
//it easy to change color
-(void)your_method
{
// your actions
[tabItem setBackgroundColor:[UIColor redColor]];
}
#define SYSTEM_OS [[[UIDevice currentDevice] systemVersion] intValue]
- (void) setTabBarColors {
if (SYSTEM_OS >= 5) {
self.tabBarController.tabBar.tintColor = [UIColor blueColor];
self.tabBarController.tabBar.selectedImageTintColor = [UIColor magentaColor];
__block NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"ArialMT" size:12.0f], UITextAttributeFont,
[UIColor lightGrayColor], UITextAttributeTextColor,
nil];
__block NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"ArialMT" size:12.0f], UITextAttributeFont,
[UIColor whiteColor], UITextAttributeTextColor,
nil];
[self.tabBarController.viewControllers enumerateObjectsUsingBlock:^(UIViewController * obj, NSUInteger idx, BOOL *stop) {
[obj.tabBarItem setTitleTextAttributes:dict1 forState:UIControlStateNormal];
[obj.tabBarItem setTitleTextAttributes:dict2 forState:UIControlStateSelected];
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.