【问题标题】:UIBarButtonItem with UIImage too wide带有 UIImage 的 UIBarButtonItem 太宽
【发布时间】:2013-03-19 10:28:32
【问题描述】:

我有一个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 点):

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

【问题讨论】:

  • 也许是灵活的空间造成了问题
  • 不,不带空格检查。
  • 那一定是图片,可以拉伸但不能缩小。在将图像设置在 UIToolbar 上之前,使用@iPatel 留下的答案来调整图像大小。 [self setBackgroundImage:resizedImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  • 当然会缩小!我将引用上面的文字:“在 UINavigationBar 中,项目看起来很好,尺寸最佳等等。”
  • 尝试为栏按钮项设置宽度,默认为0,宽度与图像宽度一样大。我想不出别的了

标签: ios uinavigationbar uitoolbar


【解决方案1】:

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

barButtonItem.width = 40f;

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

【讨论】:

    【解决方案2】:

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

    使用以下方法与特定 hightwidthimage

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

    【讨论】:

    • 我不是在寻找图像大小调整,因为这就是 resizableImageWithCapInsets: 的用途。问题远不止于此,UIToolbar 不会像 UINavigationBar 那样压缩 UIBarbuttonItem 的宽度。
    • 其他选项是删除 UIBarButtonItem 的 fixSize 并根据需要设置每个 UIBarButtonItem :)
    • AFAIK 没有办法,但请证明我错了。但请不要建议自定义视图。
    【解决方案3】:

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

    //  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];
    }
    

    【讨论】:

    • 谢谢,但是当 UIToolbar 应该开箱即用地处理这个问题时,自定义按钮不是一个选项。如果真的没有其他方法,我宁愿承认这是一个错误并生成一个更小的可拉伸图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多