【发布时间】:2012-10-30 16:36:28
【问题描述】:
我的应用程序有问题。我在 UITabBar 中的一个选项卡上设置了一个徽章值。徽章值正确为红色,徽章值周围的圆圈正确为白色。问题是,文本的颜色是灰色(160、160、160)。它与正常状态 tabbaritem 文本的颜色相同,但我在应用程序代码中没有设置此颜色,我不知道此颜色来自何处。 几周以来,我在整个网络中搜索了该问题,但找不到任何解决方案。我到处找到的唯一答案是,无法更改徽章值文本的颜色。但如果不可能,为什么在我的应用程序中改变它? 我希望有人可以帮助我解决这个问题...
- http://www.luventas-webdesign.de/stackoverflow/screenshot_badgevalue.png
- 就像我的应用程序中的颜色一样
http://www.luventas-webdesign.de/stackoverflow/screenshot_like_it_should.png
- 颜色应该是正常的......
编辑 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]];
设置徽章值:
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
【问题讨论】:
-
你有截图吗?使用 Cmd+Shift+4 拍摄快照,并将其添加到您的问题中。谢谢
-
谢谢。不知道为什么会这样。您能否分享您如何创建 UITabBar 以使其看起来像它一样,也许缺少某些东西?
-
我添加了代码 sn-ps 如何创建 TabBarController。它tabBar本身是由TabBarController自动初始化的...
-
我创建了一个新的“选项卡式应用程序”项目,并将尽可能多的代码粘贴到其中。抱歉,我的徽章文字变成了白色,完全正常。我不能让它看起来像你的。所以我猜你的问题不在你发布的代码中。
-
我现在发现,灰色文本颜色的原因是覆盖了 UILabel,但我不明白,为什么......我将它覆盖为垂直对齐。我将代码添加到主帖中。
标签: objective-c ios uitabbar uicolor uitabbaritem