How to adjust an UIButton's imageSize?

前端 未结 16 2127
时光说笑
时光说笑 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题