ios 如何以编程方式调整 UIButton 内的图像大小?

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

How to resize an image inside an UIButton programmatically?

iosswiftxcodeswift3xcode8

提问by Elia Crocetta

I have this UIButton and an image to fit in. I don't want that the image take all the space inside the button but just a little part of it right in the center, but if I resize the button it will resize the image too. How can I do that, is there an option to set whatever dimension I want independently from the size of the UIButton? Thanks!

我有这个 UIButton 和一个适合的图像。我不希望图像占据按钮内的所有空间,而只是在中心的一小部分,但是如果我调整按钮的大小,它也会调整图像的大小. 我该怎么做,是否可以选择独立于 UIButton 的大小来设置我想要的任何尺寸?谢谢!

回答by KrishnaCA

This can be done through code in the following way:

这可以通过以下方式通过代码完成:

    let imageSize:CGSize = CGSize(width: 20, height: 20)

    let button:UIButton = UIButton(type: UIButton.ButtonType.custom)
    button.frame = CGRect(x: 200, y: 200, width: 60, height: 60)
    button.backgroundColor = UIColor.yellow
    button.setImage(UIImage(named: "chat.png"), for: UIControl.State.normal)

    // The below line will give you what you want
    button.imageEdgeInsets = UIEdgeInsets(
        top: (button.frame.size.height - imageSize.height) / 2,
        left: (button.frame.size.width - imageSize.width) / 2,
        bottom: (button.frame.size.height - imageSize.height) / 2,
        right: (button.frame.size.width - imageSize.width) / 2)

    self.view.addSubview(button)

This way, you can achieve what you wanted.

这样,您就可以实现您想要的。

回答by Maciej Chrzastek

You can experiment with image view insets. Every UIButton has a property imageView.

您可以尝试使用图像视图插图。每个 UIButton 都有一个属性 imageView。

In Swift 3 you can do this like so:

在 Swift 3 中,你可以这样做:

//let button = UIButton()
button.imageView?.backgroundColor = UIColor.red
button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

red background is just so you know what is changing

红色背景只是让你知道发生了什么变化

回答by KVISH

I would do it this way:

我会这样做:

A UIButtonis just a UIView. You can simply add a UIImageViewwith a set image and call addSubviewon the UIButton.

AUIButton只是一个UIView. 你可以简单地添加UIImageView了一组图像和呼叫addSubviewUIButton

回答by Joule87

Taking into account what KVISH said before i have implemented this and it worked as expected. I posted this because Houman asked for an example.

考虑到 KVISH 在我实施之前所说的话,它按预期工作。我发布这个是因为侯曼要求举个例子。

//grab the image using the name of the pic
var image = UIImage(named: "picture")

//set the size for the image
image = image?.resize(toWidth: 18)
image = image?.resize(toHeight: 18)

//set the image to the button
buttonName.setImage(image, for: UIControlState.normal)

//adjust the position
buttonName.imageEdgeInsets = UIEdgeInsetsMake(8,16,9,0)

回答by Madavaram Ramesh

These can be achieved by adding imageEdgeInsets to a UIButton.

这些可以通过将 imageEdgeInsets 添加到 UIButton 来实现。

In swift4.2

在 swift4.2

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