4

In my application I want to change the color of bacBarButtonItem. Is it possible to change the color? or I have to put an image of it. and in case of image tell me the code how to put the image.

iPhone
  • 4,092
  • 3
  • 34
  • 58

4 Answers4

10

If you just want to change the color you can do it with this line of code.

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

Replace redColor with the following to adjust the color of the buttons:

colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this

Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color.

SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 2
    "Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color." is so important and clarifies many things. – phikes Jan 29 '14 at 15:44
  • @kkendall: what if i want to set the tint color for only the back bar button (which is set to the default system generated bar button) rather than all the Bar Button items? – AceN Jan 26 '16 at 14:54
4

Praveen-K answer is right, but take in mind that you'll have to do it in every viewcontroller.

Starting at iOS5, Apple have introduced the "appearance" concept.

- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;

In your case would be something like this

UIImage *image = [UIImage imageNamed:@"imageName.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

But as I said, Praveen-K answer is ok and will work, but just to let you know for the future.

Yorxxx
  • 679
  • 3
  • 4
3
UIImage *image = [UIImage imageNamed:@"imageName.png"];
UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backButtonAction)];
self.navigationItem.leftBarButtonItem=backBarButton;
[backBarButton release];
Praveen-K
  • 3,401
  • 1
  • 23
  • 31
0

Another way to change the color of back bar button item is to use segment control

UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", nil]] autorelease];
button.frame = CGRectMake(0, 0, 60, 30);
button.center = self.view.center;
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor colorWithRed:0 green:0.1 blue:0.5 alpha:0];
[button addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

Notice that we assign the color we want to the property tintColor of UISegmentedControl. I got the idea from this site: http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem

BB.
  • 707
  • 2
  • 11
  • 22