【问题标题】:How to change UIBarButtonItem image size如何更改 UIBarButtonItem 图像大小
【发布时间】:2014-12-05 20:01:28
【问题描述】:

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

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

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

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

有什么解决办法吗?谢谢!

【问题讨论】:

    标签: iphone objective-c ios uibarbuttonitem uinavigationitem


    【解决方案1】:

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

    1. 打开 UIBarbuttonItem 的大小检查器
    2. 更改“Bar Item”的值--> Image Inset--> top/bottom/left/right。

    试一试……

    【讨论】:

    • 这非常适合我。尤其适用于 .PDF 图像
    【解决方案2】:

    您可以使用自定义的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。

    【讨论】:

    • 不错的代码,但这不是我要找的。我不想“绘制”自定义按钮。我想要一个经典的 barbuttonItem,里面的图片比默认情况下画的要小一点。
    【解决方案3】:

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

    观看this anser 了解更多信息。

    【讨论】:

    • 感谢您的回答,但按钮大小很好,实际上很完美。这是我想要更小的内部绘制的图像。
    猜你喜欢
    • 2017-08-21
    • 2014-10-31
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多