5

Using UIBarbuttonItem, initWithImage I get a picture that I want smaller.

I feel like there is absolutely no way to resize the image.

UIedgeInsetMake does not operate at all. Resizing the picutre does not operate either (pixelate). I have an @2x 48x48 icon and a normal 24x24. creating a new picture with bigger empty border does not operate.

If I use a 20x20 it will pixelate. no matter what.

Any solution? Thanks!

Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81

3 Answers3

8

This can be achieved by the following,

  1. open size inspector of UIBarbuttonItem
  2. change the values for "Bar Item"--> Image Inset--> top/bottom/left/right.

Give it a try...

Satyaranjan
  • 137
  • 1
  • 9
3

You can use the custom BarbuttonItem to set the image and adjust the size with the title size by using the bellow method:

+(UIBarButtonItem *)createToolBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Since the buttons can be any width we use a thin image with a stretchable center point
UIImage *buttonImage = [[UIImage imageNamed:@"toolbarbutton_button_mouseup.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:@"toolbarbutton_button_mouseover.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0f]];
//[[button titleLabel] setFont:[UIFont fontWithName:@"Futura-Medium" size:12.0]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

CGRect buttonFrame = [button frame];
buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;

//Applicable only for iPhone FrameFun
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [[[NSUserDefaults standardUserDefaults] stringForKey:@"is_Landscape"] isEqualToString:@"landscape"]) {
    buttonFrame.size.height = 23.0;
}else {

    buttonFrame.size.height = buttonImage.size.height;
}

[button setFrame:buttonFrame];

[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

[button setTitle:t forState:UIControlStateNormal];

[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [buttonItem autorelease];

}

This method let you to make the dynamic size of button with desire image. Here I have used stretchableImageWithLeftCapWidth to adjust the image. I think it will help you. You can use the whole method to make Custom BarButton too.

kallol
  • 319
  • 1
  • 13
  • nice code but it's not what I'm looking for. I dont want to "draw" a custom button. I want a classic barbuttonItem with a picture inside just a bit smaller than what is drawn inside by default. – Nicolas Manzini May 24 '12 at 07:50
1

If you want to change the size of the button to match the image it might be better to create a UIButton and make the UIBarButtonItem custom. In UIBarButtonItem initWithImageThe image is scaled to fit the UIBarButtonItem.

Watch this anser for more information on how to do that.

Community
  • 1
  • 1
user373455
  • 12,675
  • 4
  • 32
  • 46