27

我正在尝试更改 UINavigationControllerBar 中后退按钮上文本的字体颜色

    [[UIBarButtonItem appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

给我这个错误:[_UIBarItemAppearance setTitleColor:forState:]: unrecognized selector sent to instance 0x69aeb70'

有什么帮助吗?谢谢!

4

5 回答 5

50
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

似乎工作!

于 2012-04-06T20:47:42.193 回答
18
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                     [UIFont fontWithName:kDefaultFont size:16.0f],UITextAttributeFont,
                                                     nil] forState:UIControlStateNormal];
于 2013-06-17T12:19:11.287 回答
15

改用这个,默认功能在 ios 5 中可用

UIBarButtonItem *backbutton =  [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];    

    [backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont,
                                                   nil] forState:UIControlStateNormal]; 
于 2012-05-04T14:19:47.767 回答
6

还有漂亮的解决方案,iOS7+(因为属性名称):

NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];

[[UIBarButtonItem appearance] setTitleTextAttributes:@{
        NSFontAttributeName: [UIFont systemFontOfSize:24],
        NSForegroundColorAttributeName: [UIColor colorWithWhite:0.2 alpha:1.0],
        NSShadowAttributeName: shadow,
} forState:UIControlStateNormal];
于 2014-06-18T04:37:04.873 回答
3

Swift 4 中的解决方案:

UIBarButtonItem.appearance().setTitleTextAttributes(
[
    NSAttributedStringKey.font: UIFont(name: "MyriadPro-SemiboldCond", size: 16)!,
    NSAttributedStringKey.foregroundColor: UIColor.white
], for: .normal)

将此添加到 AppDelegate 中,它将应用于应用程序中的所有按钮。

于 2018-03-01T09:33:48.333 回答