带有UIImage的UIBarButtonItem太宽

时间:2013-03-19 10:28:32

标签: ios uinavigationbar uitoolbar

我有一个UINavigationBar和一个UIToolbar(在不同的视图中),其中包含按钮和灵活的空间。 UIBarButtonItems'背景设置如下:

bgImage = [[UIImage imageNamed:@"MyBackgroundImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[self setBackgroundImage:bgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UINavigationBar内,项目看起来很好,最佳尺寸和所有。

但是,在UIToolbar内,项目的长度始终至少为bgImage的宽度(在这种情况下为100点):

enter image description here enter image description here

任何想法为什么或如何解决?如果您需要更多信息,请告诉我。

3 个答案:

答案 0 :(得分:2)

这似乎是UIToolbar中的一个错误。我试了一下,唯一对我有用的“修复”就是手动设置UIBarButtonItem的宽度:

barButtonItem.width = 40f;

这适用于带图像的按钮,但不适用于文本按钮,因为文本大小可能因本地化而有所不同。

答案 1 :(得分:0)

您可以根据需要使用以下方法获取/创建图片大小

使用以下方法将特定宽度图像

一起使用
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

此方法返回 NewImage ,具有您指定的特定大小 并使用此图片添加UIBarButtonItem

答案 2 :(得分:0)

根据您的要求创建自己的图像按钮

//  UIBarButtonItem+CutomBackground.h

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString *)buttonTitle target:(id)target action:(SEL)action;

//  UIBarButtonItem+CutomBackground.m

#define TEXT_MARGIN 8.0f
#define ARROW_MARGIN 12.0f
#define FONT_SIZE 13.0f
#define IMAGE_HEIGHT 31.0f

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString *)buttonTitle target:(id)target action:(SEL)action
{       
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *buttonImage = [image stretchableImageWithLeftCapWidth:15 topCapHeight:0];    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [button setContentEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f,TEXT_MARGIN)];
    [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT_SIZE]];
    [button.titleLabel setShadowOffset:CGSizeMake(0.0f,-1.0f)];
    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = [buttonTitle sizeWithFont:[button.titleLabel font]].width+ARROW_MARGIN+TEXT_MARGIN;
    buttonFrame.size.height = IMAGE_HEIGHT;
    [button setFrame:buttonFrame];
    return [[self alloc] initWithCustomView:button];
}