How to adjust an UIButton's imageSize?

前端 未结 16 2097
时光说笑
时光说笑 2021-01-29 21:40

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

[myLikesButton setImage:[UIImage imageNamed:@\"icon-heart.png\"] forState:UICo         


        
相关标签:
16条回答
  • 2021-01-29 22:26

    Swift 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)
    
    0 讨论(0)
  • 2021-01-29 22:28

    Updated for Swift 3

    yourButtonName.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    
    0 讨论(0)
  • 2021-01-29 22:31

    Here is the other solution to scale an imageView of UIButton.

    button.imageView?.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0.8)
    
    0 讨论(0)
  • 2021-01-29 22:35

    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.

    0 讨论(0)
提交回复
热议问题