如何调整大图像的大小并使其适应swift中的UIButton?

时间:2022-10-12 21:14:05

I am trying to place a large image on a button after resizing to the exact size of button. I am making use of this code

我想在调整按钮的确切大小后,在按钮上放置一个大图像。我正在使用这段代码

var FB = UIButton(type:UIButtonType.Custom) as UIButton
var image = UIImage(named: "facebook-logo-png-2.png")
func buttonTouchDown(sender:UIButton!){
    print("button Touch Down")
}

 override func viewDidLoad() {
    super.viewDidLoad()

  FB.frame = CGRectMake(0,0 , 100, 100)
    FB.backgroundColor = UIColor.clearColor()
    FB.addTarget(self, action: #selector(buttonTouchDown(_:)), forControlEvents: .TouchDown)
    FB.setImage(image, forState: .Normal)
    FB.imageEdgeInsets = UIEdgeInsetsMake(25,25,25,25)
    self.view.addSubview(FB)}

I am not getting the desired output. Currently, my output looks like this screen shot 如何调整大图像的大小并使其适应swift中的UIButton? This is my first iOS app, please help me if possible

我没有得到所需的输出。目前,我的输出看起来像这个屏幕截图这是我的第一个iOS应用程序,请尽可能帮助我

Thanks in advance

提前致谢

1 个解决方案

#1


0  

Use this line

使用此行

FB.contentMode = .ScaleAspectFit

FB.contentMode = .ScaleAspectFit

Before self.view.addSubview(FB)

What it does is scale the image to the fit the size while being true to the aspect ratio of the image being set.

它的作用是将图像缩放到适合的大小,同时保持正在设置的图像的宽高比。

#1


0  

Use this line

使用此行

FB.contentMode = .ScaleAspectFit

FB.contentMode = .ScaleAspectFit

Before self.view.addSubview(FB)

What it does is scale the image to the fit the size while being true to the aspect ratio of the image being set.

它的作用是将图像缩放到适合的大小,同时保持正在设置的图像的宽高比。