iosuiimageuinavigationbaruitoolbaruitoolbaritem

Set background image of UIBarButtonItem programmatically changes its size


I've been able to have a custom UIBarButtonItem with an embedded uibutton through story board. It's the map button. see parameters on this screenshot, I had to use background property instead of Image.

map bar button

But when I tried to customize some uibarbuttons programmatically, then the buttons get smaller. I had the same result with the map button when I was using Image property.

Here the code i'm writing for the back button,

UIImage *backButtonImage = [[UIImage imageNamed:@"Retour.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(21, 21, 21, 21)] ;
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 button.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[button setBackgroundImage:backButtonImage forState:UIControlStateHighlighted];

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:button] ;
self.navigationItem.leftBarButtonItem = backBarButton;

I tried with or without resizableImageWithCapInsets, stretchableImageWithLeftCapWidth with the same result below (the back button should have the same size as the map button)

I tried also iOS 5 setBackButtonBackgroundImage methods but the button was not customized at all.

navigation toolbar


Solution

  • Well the problem was that I had overwritten the Retour@2x.png for retina display with Retour.png -__-'