如何在UIButton中拉伸图像

时间:2022-07-09 21:06:41

I'm trying to create a custom UITableViewCell programmatically and one of the subviews of this cell is going to be a button with an image in it (a simple image of a magnifying glass). However, I want the button's image to be centered and scaled proportionately down to fit in the button and NOT to be stretched to fill the entire button. Below is my code where self refers to the custom UITableViewCell which I am placing the button into.

我正在尝试以编程方式创建自定义UITableViewCell,并且此单元格的一个子视图将是一个带有图像的按钮(放大镜的简单图像)。但是,我希望按钮的图像居中并按比例缩小以适应按钮,而不是拉伸以填充整个按钮。下面是我的代码,其中self指的是我将按钮放入的自定义UITableViewCell。

self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton setBackgroundImage:[UIImage imageNamed: @image_name_here"] forState:UIControlStateNormal];
self.myButton.frame = CGRectMake(...//something, something)
self.myButton.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.mySearchHelpButton];

Right now the image stretches to fill the entire button rather than scaling proportionately so that it fits nicely.

现在,图像会拉伸以填充整个按钮,而不是按比例缩放,以使其非常适合。

I have also tried setting the contentMode to UIViewContentModeScaleAspectFill but this doesn't seem to change anything. In fact, none of the different contentModes seem to change anything.

我也尝试将contentMode设置为UIViewContentModeScaleAspectFill,但这似乎没有改变任何东西。实际上,没有任何不同的内容模式似乎会改变任何东西。

5 个解决方案

#1


61  

Have you tried setImage instead of setBackgroundImage?

你尝试过setImage而不是setBackgroundImage吗?

#2


41  

Make sure the button is a Custom UIButton

确保该按钮是Custom UIButton

Just using setImage: did not work for me on iOS9.

只是使用setImage:在iOS9上对我不起作用。

But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;

但它与myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit结合使用;

#3


30  

In Swift...

在斯威夫特...

button.imageView?.contentMode = .aspectFit

#4


18  

Storyboard

Property image.

物业形象。

You must define specific property in Runtime Attributes of Identity inspectorimageView.contentMode, where you set value relatively to rawValue position of enum UIViewContentMode. 1 means scaleAspectFit.

您必须在Identity检查器的运行时属性中定义特定属性 - imageView.contentMode,您可以在其中相对于枚举UIViewContentMode的rawValue位置设置值。 1表示scaleAspectFit。

如何在UIButton中拉伸图像

And button's alignment, in Attributes inspector:

和按钮的对齐,在属性检查器中:

如何在UIButton中拉伸图像

#5


4  

Swift Version

Swift版本

myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit

#1


61  

Have you tried setImage instead of setBackgroundImage?

你尝试过setImage而不是setBackgroundImage吗?

#2


41  

Make sure the button is a Custom UIButton

确保该按钮是Custom UIButton

Just using setImage: did not work for me on iOS9.

只是使用setImage:在iOS9上对我不起作用。

But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;

但它与myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit结合使用;

#3


30  

In Swift...

在斯威夫特...

button.imageView?.contentMode = .aspectFit

#4


18  

Storyboard

Property image.

物业形象。

You must define specific property in Runtime Attributes of Identity inspectorimageView.contentMode, where you set value relatively to rawValue position of enum UIViewContentMode. 1 means scaleAspectFit.

您必须在Identity检查器的运行时属性中定义特定属性 - imageView.contentMode,您可以在其中相对于枚举UIViewContentMode的rawValue位置设置值。 1表示scaleAspectFit。

如何在UIButton中拉伸图像

And button's alignment, in Attributes inspector:

和按钮的对齐,在属性检查器中:

如何在UIButton中拉伸图像

#5


4  

Swift Version

Swift版本

myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit