ios 如何缩放 UIButton 的 imageView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:43:17  来源:igfitidea点击:

How do I scale a UIButton's imageView?

iosuibuttonscale

提问by vcLwei

I created a UIButton instance named "button" with an image using [UIButton setImage:forState:]. The button.frame is larger than the image's size.

我使用[UIButton setImage:forState:]. button.frame 大于图像的大小。

Now I want to scale this button's image smaller. I tried changing button.imageView.frame, button.imageView.boundsand button.imageView.contentMode, but all seem ineffective.

现在我想缩小这个按钮的图像。我尝试更改button.imageView.frame,button.imageView.boundsbutton.imageView.contentMode,但似乎都无效。

Can anyone help me scale a UIButton's imageView?

任何人都可以帮助我缩放 aUIButton的 imageView 吗?

I created the UIButtonlike this:

我创建了UIButton这样的:

UIButton *button = [[UIButton alloc] init];
[button setImage:image forState:UIControlStateNormal];

I tried to scale the image like this:

我试图像这样缩放图像:

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.bounds = CGRectMake(0, 0, 70, 70);

and this:

和这个:

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.frame = CGRectMake(0, 0, 70, 70);

回答by Chris

For the original poster, here is the solution I found:

对于原始海报,这是我找到的解决方案:

commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

This will allow your button to scale horizontally. There is a vertical setting as well.

这将允许您的按钮水平缩放。还有一个垂直设置。

Took me several hours to figure that one out (the naming of the property is veryunintuitive) so figured I'd share.

我花了几个小时才弄明白(属性的命名非常不直观),所以我想分享一下。

回答by Hitesh Savaliya

I'd faced similar problem, where I've a background Image (one with border) and an image (flag) for a custom button. I wanted flag to be scaled down and in center. I tried changing imageView's attribute but didn't succeed and was seeing this image --

我遇到过类似的问题,我有一个背景图像(一个带边框的)和一个自定义按钮的图像(标志)。我希望标志按比例缩小并居中。我尝试更改 imageView 的属性但没有成功并且看到了这张图片——

enter image description here

在此处输入图片说明

During my experiments, I tried :

在我的实验中,我尝试了:

button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)

I achieved expected result as :

我达到了预期的结果:

enter image description here

在此处输入图片说明

回答by Linh Nguyen

An Addition Way for config in XIB file. You can choose this option if you want text or image is Scale full fill in UIButton. It's will be the same with code.

XIB 文件中配置的一种附加方式。如果您希望文本或图像在 UIButton 中缩放完全填充,则可以选择此选项。代码也是一样。

UIButton *btn = [UIButton new];

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment   = UIControlContentVerticalAlignmentFill;

enter image description here

在此处输入图片说明

回答by pavelpanov

use

button.contentMode = UIViewContentModeScaleToFill;

not

不是

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

Update:

更新:

As @Chris commented,

正如@Chris 评论的那样,

To get this to work for setImage:forState:, you need to do the following to scale horizontally: myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

要使其适用于 setImage:forState:,您需要执行以下操作来水平缩放: myButton.contentHorizo​​ntalAlignment = UIControlContentHorizo​​ntalAlignmentFill;

回答by EEE

    UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,70)];
    button.buttonType = UIButtonTypeCustom;
    UIImage *buttonImage = [UIImage imageNamed:@"image.png"];

    UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
    [button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal]; 

回答by marcio

I found this solution.

我找到了这个解决方案。

1) Subclass the following methods of UIButton

1)子类化以下方法 UIButton

+ (id)buttonWithType:(UIButtonType)buttonType {
    MyButton *toReturn = [super buttonWithType:buttonType];
    toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return toReturn;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return contentRect;
}

And it works well.

它运作良好。

回答by Hlung

Strange, the only combo that worked for me (iOS 5.1) is...

奇怪的是,唯一对我有用的组合(iOS 5.1)是......

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

and

[button setImage:newImage forState:UIControlStateNormal];

[button setImage:newImage forState:UIControlStateNormal];

回答by JosephH

I just ran into this same problem, and there's a possible answer in this question:

我刚刚遇到了同样的问题,这个问题有一个可能的答案:

Why does a custom UIButton image does not resize in Interface Builder?

为什么自定义 UIButton 图像不会在 Interface Builder 中调整大小?

Essentially, use the backgroundimage property instead, which does get scaled.

本质上,改用 backgroundimage 属性,它会被缩放。

回答by Sandip Patel - SM

Just do (From Design OR From Code):

只是做(从设计或从代码):

From Design

从设计

  1. Open your xib OR Storyboard.
  2. Select button
  3. Inside Attribute Inspector(Right side) > In "Control"section > select last 4th optionfor both Horizontal and Verical.
  1. 打开您的 xib 或故事板。
  2. 选择按钮
  3. 属性检查器内部(右侧)> 在“控制”部分>为水平和垂直选择最后一个第四个选项

[For Point#3: Change Horizontal and vertical Align to UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]

[对于第 3 点:将水平和垂直对齐更改为 UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]

From Code

从代码

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment =  UIControlContentVerticalAlignmentFill;

回答by huangkui

like this can solve your problem:

像这样可以解决您的问题:

+ (UIImage*)resizedImage:(UIImage*)image
{
 CGRect frame = CGRectMake(0, 0, 60, 60);
 UIGraphicsBeginImageContext(frame.size);
 [image drawInRect:frame];
 UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return resizedImage;
}