为什么我不能使用自己的 UIColor 来更改 UITabBarIte 的标题颜色?

WHY cannot I use my own UIColor to change the UITabBarIte's title color?

我尝试设置UITabBarItem的title的颜色使用方法如下:

UIColor *sColor = [UIColor colorWithRed:1/255.0 green:161/255.0 blue:171/255.0];
UITabBarItem *b_item = [[UITabBarItem alloc] initWithTitle:@"test" image:normal_image selectedImage:selected_image];
[b_item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:sColor, NSForegroundColorAttributeName, [UIFont systemFontOfSize:13], NSFontAttributeName, nil] forState:UIControlStateSelected];

但是这些代码没有任何意义,一旦我更改了代码:

UIColor *sColor = [UIColor colorWithRed:1/255.0 green:161/255.0 blue:171/255.0];

进入那些代码:

UIColor *sColor = [UIColor redColor];

并且此代码有效。 我不知道为什么我不能使用我自己定义的颜色value.Please给我一些帮助!

最后添加alpha:1.0,

[UIColor colorWithRed:1/255.0 green:161/255.0 blue:171/255.0 alpha:1.0];

尝试像这样将 alpha 添加到方法中:

UIColor *sColor = [UIColor colorWithRed:1/255.0 green:161/255.0 blue:171/255.0 alpha:1.0];

根据用户评论修改的答案

如果不是 alpha,那么可能是您为 barbuttonitem 设置颜色的方式?

[[UITabBarItem appearance] setTitleTextAttributes:@{
    NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0f],                                                         
    NSForegroundColorAttributeName : [UIColor colorWithRed:1/255.0 green:161/255.0 blue:171/255.0 alpha:1]
    } forState:UIControlStateNormal];

请注意,我已将 forState 设置为 UIControlStateNormal。在你的问题中,它是 UIControlStateSelected ,它只有在用户选择按钮时才有效。