How to adjust an UIButton's imageSize?

前端 未结 16 2260
时光说笑
时光说笑 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

    Tim's answer is correct, however I wanted to add another suggestion, because in my case there was a simpler solution altogether.

    I was looking to set the UIButton image insets because I didn't realize that I could set the content mode on the button's UIImageView, which would have prevented the need to use UIEdgeInsets and hard-coded values altogether. Simply access the underlying imageview on the button and set the content mode:

    myButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    See UIButton doesn't listen to content mode setting?

    Swift 3

    myButton.imageView?.contentMode = .scaleAspectFit
    

提交回复
热议问题