objective-c - UITabbarItem BadgeValue 文本颜色

标签 objective-c ios uitabbar uicolor uitabbaritem

我的应用程序有问题。我在 UITabBar 的其中一个选项卡上设置了一个角标(Badge)值。角标(Badge)值正确为红色,角标(Badge)值周围的圆圈正确为白色。问题是,文本的颜色是灰色 (160, 160, 160)。它与正常状态的 tabbaritem 文本颜色相同,但我在应用程序代码中没有设置这种颜色,我不知道这种颜色来自哪里。 几个星期以来,我在整个网络中搜索了该问题,但找不到任何解决方案。我在各处找到的唯一答案是,无法更改角标(Badge)值文本的颜色。但如果不可能,为什么它在我的应用程序中发生了变化? 我希望有人可以帮助我解决这个问题......


编辑 02.11.2012 - 代码

TabBarController 的创建:

#import "ExtendedTabBarController.h"
#import "configuration.h"

@implementation ExtendedTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:207.0/255.0 green:70.0/255.0 blue:61.0/255.0 alpha:1], UITextAttributeTextColor, [UIFont fontWithName:@"KievitPro-Regular" size:10.0], UITextAttributeFont, nil] forState:UIControlStateSelected];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1], UITextAttributeTextColor, [UIFont fontWithName:@"KievitPro-Regular" size:10.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

    [self.tabBar sizeToFit];

    UIView *tabbarBackgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0, self.view.bounds.size.width, 49)];
    [tabbarBackgroundColorView setBackgroundColor:[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1]];
    [self.tabBar insertSubview:tabbarBackgroundColorView atIndex:0];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsPortrait(interfaceOrientation); // only portrait orientation

}

/**
 *  orientation for iOS6
 **/
-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

@end

在 AppDelegate 中调用:

ExtendedTabBarController *tabBarController = [[ExtendedTabBarController alloc] init];
[self setTabBarController:tabBarController];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"menu_bg"]];

// code for initialize View- and NavigationControllers...

self.tabBarController.viewControllers = @[highlightsNavigationController, categoryNavigationController, searchNavigationController, favoritesNavigationController, imprintNavigationController];

self.window.rootViewController = self.tabBarController;

[[UITabBar appearance] setSelectionIndicatorImage:[[UIImage alloc] init]];

设置角标(Badge)值:

int viewCount = 0;
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
    if([key rangeOfString:@"_highlighted"].location != NSNotFound && [[[dict objectForKey:key] objectAtIndex:0] isEqualToString:@"YES"]) {
        viewCount++;
    }
}
UITabBarItem *tbi = (UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:3];
if(viewCount <= 0) {
    tbi.badgeValue = nil;
} else {
    tbi.badgeValue = nil;
    tbi.badgeValue = [NSString stringWithFormat:@"%d", viewCount];
}

覆盖 UILabel 的代码:

// -- file: UILabel+VerticalAlign.h
#pragma mark VerticalAlign
@interface UILabel (VerticalAlign)
- (void)alignTop;
- (void)alignBottom;
- (void)awakeFromNib;
-(id)initWithFrame:(CGRect)frame;
@end


#import "UILabel+VerticalAlign.h"

// -- file: UILabel+VerticalAlign.m
@implementation UILabel (VerticalAlign)
- (void)alignTop {
    CGSize fontSize = [self.text sizeWithFont:self.font];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [self.text stringByAppendingString:@"\n "];
}

- (void)alignBottom {
    CGSize fontSize = [self.text sizeWithFont:self.font];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
}

-(id)initWithFrame:(CGRect)frame
{
    id result = [super initWithFrame:frame];
    if (result) {
        [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
    }
    return result;
}

@end

最佳答案

我自己找到了解决问题的办法:

我必须从覆盖的 UILabel 中删除以下行:

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
}

-(id)initWithFrame:(CGRect)frame
{
    id result = [super initWithFrame:frame];
    if (result) {
        [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
    }
    return result;
}

也许有人可以解释一下,为什么这行会在我们关闭此帖子之前更改角标(Badge)值的文本颜色?

关于objective-c - UITabbarItem BadgeValue 文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13143646/

相关文章:

ios - 传递给另一个类时,NSString 的 id 返回 null

objective-c - 如何在控制设备方向的同时控制设备状态栏的方向?

ios - 找不到实例方法 '-_setWebGLEnabled:' (返回类型默认为 'id' )

iphone - iPhone ObjC 函数中的可变参数

ios - Swift 组合 : Run a list of publishers one after the other, 并发布第一个非零元素

ios - Oauth1 错误常量 Swift iO

ios - 通过包含 UIScrollView 的滚动重置 UIViews 的框架

ios - 将 uitabbar 实现到 uitableview

iphone - iOS 5 跨多个场景保持一致的标签栏

iphone - 动态更改选项卡的 Nib