如何更改UIBarButtonItem图像大小

[英]How to change UIBarButtonItem image size


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

使用UIBarbuttonItem,initWithImage我得到一张我想要更小的图片。

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.

UIedgeInsetMake根本不运行。调整图片的大小也不起作用(像素化)。我有一个@ 2x 48x48图标和一个普通的24x24。创建具有较大空边框的新图片不起作用。

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

如果我使用20x20它会像素化。无论。

Any solution? Thanks!

有解决方案吗谢谢!

3 个解决方案

#1


5  

This can be achieved by the following,

这可以通过以下方式实现,

  1. open size inspector of UIBarbuttonItem
  2. UIBarbuttonItem的开放大小检查器
  3. change the values for "Bar Item"--> Image Inset--> top/bottom/left/right.
  4. 更改“条形图” - >图像插入 - >上/下/左/右的值。

Give it a try...

试一试...

#2


3  

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

您可以使用自定义BarbuttonItem设置图像,并使用bellow方法调整标题大小:

+(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.

此方法允许您使用所需图像制作按钮的动态大小。这里我使用了stretchableImageWithLeftCapWidth来调整图像。我认为它会对你有所帮助。您也可以使用整个方法来制作Custom BarButton。

#3


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.

如果要更改按钮的大小以匹配图像,最好创建UIButton并使UIBarButtonItem自定义。在UIBarButtonItem中,initWithImage缩放图像以适合UIBarButtonItem。

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

观看此anser以获取有关如何执行此操作的更多信息。

关注微信公众号

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/05/23/50cbbacdf185886f20a7b7904c581139.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com