23

I want to change the color to red.

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • possible duplicate of [UIBarButtonItem with color?](http://stackoverflow.com/questions/664930/uibarbuttonitem-with-color) – memmons Feb 09 '12 at 18:29

6 Answers6

104

I found a simpler way to just change the color of title: iOS7:

UIBarButtonItem *button = 
 [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                  style:UIBarButtonItemStyleBordered 
                                 target:nil 
                                 action:nil];
[button setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys: 
               [UIColor redColor], NSForegroundColorAttributeName,nil] 
                                            forState:UIControlStateNormal];

and prior iOS7:

UIBarButtonItem *button = 
  [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                   style:UIBarButtonItemStyleBordered 
                                  target:nil 
                                  action:nil];
[button setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys: 
               [UIColor redColor], UITextAttributeTextColor,nil] 
                                    forState:UIControlStateNormal];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Gavy
  • 1,939
  • 1
  • 19
  • 20
9

This question was answered here. Basically, you have to create a UIButton, configure it as you wish, and then initialize the Bar Button Item with the UIButton as a custom view.

Community
  • 1
  • 1
Tim Isganitis
  • 1,554
  • 9
  • 11
8

There are Two ways to change the color of the Text displayed UIBarButtonITem.

1)

[barButtonITem setTintColor:[UIColor redColor]];

2) or you can you any UIColor class method. this is really efficient solution.Althought for iOS 7 you can use NSForegroundColorAttributeName and can use

barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
3

On similar note from Guvvy Ava you can change font size like

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
mask
  • 6,172
  • 3
  • 24
  • 23
2

swift version 4.0 or later

barButtonITem.tintColor = UIColor.red   // for textColor change

if want to change font as well as text color use the following code

barButtonITem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor :UIColor.red,NSAttributedStringKey.font:UIFont.init(name:"Avenir", size:15.0)], for: UIControlState.normal)
Sunil Singh
  • 538
  • 3
  • 12
0

If you are using system Navigation bar button item then to change your button tint color use

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

To change button text color use the following code also.

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];

Add the following line of code which viewcontroller you want to change your color

Mehedi Hasan
  • 359
  • 3
  • 10