ios 如何调整 UIButton 的 imageSize?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10576593/
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 18:21:50  来源:igfitidea点击:

How to adjust an UIButton's imageSize?

iosobjective-cxcodeuibutton

提问by adit

How can I adjust the image size of the UIButton? I am setting the image like this:

如何调整 UIButton 的图像大小?我正在像这样设置图像:

[myLikesButton setImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal];

However this fills up the image to the full button, how do I make the image smaller?

但是,这会将图像填充到完整按钮,如何使图像变小?

回答by Tim C

If I understand correctly what you're trying to do, you need to play with the buttons image edge inset. Something like:

如果我正确理解您要做什么,您需要使用按钮图像边缘插入。就像是:

myLikesButton.imageEdgeInsets = UIEdgeInsets(top, left, bottom, right)

回答by Kyle Clegg

Tim's answer is correct, however I wanted to add another suggestion, because in my case there was a simpler solution altogether.

蒂姆的回答是正确的,但是我想添加另一个建议,因为就我而言,有一个更简单的解决方案。

I was looking to set the UIButtonimage insets because I didn't realize that I could set the content mode on the button's UIImageView, which would have prevented the need to use UIEdgeInsets and hard-coded values altogether. Simply access the underlying imageview on the button and set the content mode:

我希望设置UIButton图像插入,因为我没有意识到我可以在按钮的 上设置内容模式UIImageView,这将完全不需要使用 UIEdgeInsets 和硬编码值。只需访问按钮上的底层图像视图并设置内容模式:

myButton.imageView.contentMode = UIViewContentModeScaleAspectFit;

See UIButton doesn't listen to content mode setting?

看到UIButton 不听内容模式设置吗?

Swift 3

斯威夫特 3

myButton.imageView?.contentMode = .scaleAspectFit

回答by Josh O'Connor

Swift 3:

斯威夫特 3:

button.setImage(UIImage(named: "checkmark_white"), for: .normal)
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

回答by Milos Mandic

You can also do that from inteface builder like this.

您也可以像这样从界面构建器中执行此操作。

enter image description here

在此处输入图片说明

I think it's helpful.

我认为这是有帮助的。

回答by iosjillian

If your image is too large (and you can't/don't want to just made the image smaller), a combination of the first two answers works great.

如果您的图像太大(并且您不能/不想将图像缩小),则前两个答案的组合效果很好。

addButton.imageView?.contentMode = .scaleAspectFit
addButton.imageEdgeInsets = UIEdgeInsetsMake(15.0, 15.0, 15.0, 5.0)

Unless you get the image insets just right, the image will be skewed without changing the contentMode.

除非您将图像插入恰到好处,否则图像将在不更改contentMode.

回答by safaa elgendi

you can use imageEdgeInsetsproperty The inset or outset margins for the rectangle around the button's image.

您可以使用imageEdgeInsets属性按钮图像周围矩形的插入或起始边距。

 [self.btn setImageEdgeInsets:UIEdgeInsetsMake(6, 6, 6, 6)];

A positive value shrinks, or insets, that edge—moving. A negative value expands, or outsets, that edge.

正值会缩小或插入该边缘 - 移动。负值会扩展或开始该边缘。

回答by Esqarrouth

Heres the Swift version:

这是 Swift 版本:

myButton.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

回答by ovo

Here is the other solution to scale imageView of UIButton.

这是缩放 UIButton 的 imageView 的另一种解决方案。

button.imageView?.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0.8)

回答by Van Du Tran

Swift 4

斯威夫特 4

You would need to use these two lines of code, in this specific order. All you need is to change the top and bottom value of the edge insets.

您需要按此特定顺序使用这两行代码。您只需要更改边缘插入的顶部和底部值。

addButton.imageView?.contentMode = .scaleAspectFit
addButton.imageEdgeInsets = UIEdgeInsetsMake(10.0, 0.0, 10.0, 0.0)

回答by Borys T

When changing icon size with UIEdgeInsetsMake(top, left, bottom, right), keep in mind button dimensions and the ability of UIEdgeInsetsMake to work with negative values as if they are positive.

使用 更改图标大小时 UIEdgeInsetsMake(top, left, bottom, right),请记住按钮尺寸和 UIEdgeInsetsMake 处理负值的能力,就像它们是正值一样。

Example: Two buttons with height 100 and aspect 1:1.

示例:高度为 100 且纵横比为 1:1 的两个按钮。

left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
right.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)

enter image description here

在此处输入图片说明

left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
right.imageEdgeInsets = UIEdgeInsetsMake(45, 0, 45, 0)

enter image description here

在此处输入图片说明

left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
right.imageEdgeInsets = UIEdgeInsetsMake(60, 0, 60, 0)

enter image description here

在此处输入图片说明

Examples 1 and 3 are identical since ABS(100 - (40 + 40)) = ABS(100 - (60 + 60))

示例 1 和示例 3 相同,因为 ABS(100 - (40 + 40)) = ABS(100 - (60 + 60))