How to resize UIBarButtonItem image

Using the UIBarbuttonItem, initWithImage, I get a snapshot that I want to reduce.

I feel that there is absolutely no way to resize an image.

UIedgeInsetMake doesn't work at all. Resizing picutre does not work (pixelate). I have an @ 2x 48x48 icon and a regular 24x24. Creating a new image with a larger blank border does not work.

If I use 20x20, it will be pixelated. no matter what.

Any solution? Thank!

+5
source share
3 answers

This can be achieved as follows:

  • open sibling inspector UIBarbuttonItem
  • " " → → ///.

...

+5

BarbuttonItem , :

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

}

. stretchableImageWithLeftCapWidth . , . , Custom BarButton .

+3

, UIButton UIBarButtonItem. UIBarButtonItem initWithImage , UIBarButtonItem.

anser , .

+1
source

All Articles