简体   繁体   English

如何调整大图像的大小并使它迅速适应UIButton?

[英]How to resize a large image and make it fit into UIButton in swift?

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 目前,我的输出看起来是这样的屏幕截图 在此处输入图片说明 This is my first iOS app, please help me if possible 这是我的第一个iOS应用,请尽可能帮我

Thanks in advance 提前致谢

Use this line 使用此行

FB.contentMode = .ScaleAspectFit FB.contentMode = .ScaleAspectFit

Before self.view.addSubview(FB) 在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. 它的作用是将图像缩放到适合其大小的大小,同时与设置的图像的纵横比保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM