Gav*_*avy 104

我找到了一种更简单的方法来改变标题的颜色:iOS7:

UIBarButtonItem *button = 
 [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                  style:UIBarButtonItemStyleBordered 
                                 target:nil 
                                 action:nil];
[button setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys: 
               [UIColor redColor], NSForegroundColorAttributeName,nil] 
                                            forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

和以前的iOS7:

UIBarButtonItem *button = 
  [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                   style:UIBarButtonItemStyleBordered 
                                  target:nil 
                                  action:nil];
[button setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys: 
               [UIColor redColor], UITextAttributeTextColor,nil] 
                                    forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

  • 如果你是iOS 5.0或更高版本,当然是一个很好的解决方案.OP:查看UIBarItem.h了解详细信息. (2认同)

Tim*_*tis 8

这个问题得到回答在这里.基本上,您必须创建一个UIButton,根据需要进行配置,然后使用UIButton作为自定义视图初始化Bar Button Item.


Pro*_*nja 8

有两种方法可以更改文本显示的UIBarButtonITem的颜色.

1)

[barButtonITem setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

2)或者你可以使用任何UIColor类方法.这是非常有效的解决方案.对于iOS 7,您可以使用NSForegroundColorAttributeName并且可以使用

barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)