Ignacio Oroná
Ignacio Oroná

Reputation: 4399

iOS Can't change UIBarButtonItem image properly

I'm having a problem when I try to change a UINavigationBar's "back" button custom icon. To achieve such a thing, I'm using the following code:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem* leftBtn;
    UIImage* botonVolverImg = [UIImage imageNamed:@"boton_volver.png"]; 

    leftBtn = self.navigationItem.leftBarButtonItem;
    leftBtn.image = botonVolverImg;
}

But, when the view appears, you see this:

a busy cat http://www.timotteo.com.ar/boton.png

(You can see that the old button still appears at the back, plus the image I chose looks a bit streched)

I've been changing the imageInsets property, but that doesn't seem to work. I've also been reading the forum around, but couldn´t find the exact solution.

Any suggestions are welcomed!

Upvotes: 2

Views: 1441

Answers (2)

lxt
lxt

Reputation: 31304

To supplement Mark's answer, if your customer is requiring you to support iOS 4, you could create a UIButton that looks exactly how your customer wants it to (ie, just that image), and then use UIBarButton's initWithCustomView method to create your own back button. You can then have that button trigger popViewController or whatever appropriate action you need.

Upvotes: 2

Mark Adams
Mark Adams

Reputation: 30846

The image property for UIBarButtonItem doesn't correspond to the background image, only an image that provides additional context. If you're targeting 5.0+, your best bet would be to use -setBackButtonBackgroundImage:forState:barMetrics: to set a background for the bar button item.

Upvotes: 3

Related Questions