Question

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.

Was it helpful?

Solution

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];

OTHER TIPS

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top