2012-05-23 220 views
5

使用UIBarbuttonItem,initWithImage我得到一张我想要的小图片。如何更改UIBarButtonItem图像大小

我觉得有没有办法调整图像的大小。

UIedgeInsetMake根本不起作用。调整picutre的大小也不会运行(像素)。 我有一个@ 2x 48x48图标和一个正常的24x24。创建一个更大的空白边框的新图片无法运行。

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

任何解决方案?谢谢!

回答

1

如果你想改变按钮的大小来匹配图片,最好创建一个UIButton并制作UIBarButtonItem自定义。在UIBarButtonItem中initWithImage图像被缩放以适应UIBarButtonItem。

手表this有关如何做到这一点的更多信息。

+0

谢谢您的回答,但按钮的大小是好的,实际上它是完美的。这是我想要的更小的图像。 –

3

您可以使用自定义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调整图像。我认为它会帮助你。您也可以使用整个方法来制作自定义BarButton。

+0

不错的代码,但它不是我要找的。我不想“画”一个自定义按钮。我想要一个经典的barbuttonItem,里面的图片比默认的里面的图片小一些。 –

5

这可以通过以下的UIBarButtonItem

  • 变化

    1. 开大小检查来实现,“栏项目”的价值观 - >图像插入 - >上/下/左/右。

    试试看......

  • +0

    这对我来说很完美。尤其适用于.PDF图像 –