【问题标题】:How can I change color of UITabBarItem's title when it's selecetd选择时如何更改 UITabBarItem 标题的颜色
【发布时间】:2012-10-08 08:49:47
【问题描述】:

我的环境,

iOS6
Xcode 4.5.1

我想在 UITabBarItem 被选中时更改其标题的颜色。

我将 CustomUITabBarItem 用于 UITabBarItem。 - 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;
}   

如何更改颜色?

【问题讨论】:

标签: iphone ios xcode4


【解决方案1】:
 [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
                                            [UIColor blackColor], UITextAttributeTextColor,
                                            [UIColor grayColor],        
                                            nil]];

请确保这仅适用于 iOS 5.0 或更高版本。

【讨论】:

  • 非常感谢!但是我需要在最后添加“forState:UIControlStateSelected”。反正我能做好。
【解决方案2】:
//it easy to change color
-(void)your_method
{
// your actions
[tabItem setBackgroundColor:[UIColor redColor]];
}

【讨论】:

  • 感谢您的评论,但有一个警告,“UITabView 可能无法响应 setBackgroundColor”并且命令无法正常工作。
【解决方案3】:
#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];

        }];

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多