Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

If you don't find an answer to your problem, I can suggest you this function :

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
{
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

It returns an image scaled to the specified size.

So you can resize your image at the desired size and then set it as a background image.

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