使用 UIBarbuttonItem, initWithImage 我得到一张我想要更小的图片。
我觉得绝对没有办法调整图像的大小。
UIedgeInsetMake 根本不运行。调整图片大小也不起作用(像素化)。我有一个@2x 48x48 图标和一个普通的 24x24。创建具有更大空白边框的新图片不起作用。
如果我使用 20x20,它会像素化。无论。
有什么解决办法吗?谢谢!
使用 UIBarbuttonItem, initWithImage 我得到一张我想要更小的图片。
我觉得绝对没有办法调整图像的大小。
UIedgeInsetMake 根本不运行。调整图片大小也不起作用(像素化)。我有一个@2x 48x48 图标和一个普通的 24x24。创建具有更大空白边框的新图片不起作用。
如果我使用 20x20,它会像素化。无论。
有什么解决办法吗?谢谢!
这可以通过以下方式实现,
试试看...
您可以使用自定义 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。
如果您想更改按钮的大小以匹配图像,最好创建一个 UIButton 并自定义 UIBarButtonItem。在 UIBarButtonItem initWithImage 中,图像被缩放以适合 UIBarButtonItem。
观看此分析器以获取有关如何执行此操作的更多信息。