How to adjust an UIButton's imageSize? How to adjust an UIButton's imageSize? xcode xcode

How to adjust an UIButton's imageSize?


If I understand correctly what you're trying to do, you need to play with the buttons image edge inset. Something like:

myLikesButton.imageEdgeInsets = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)


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


Swift 3:

button.setImage(UIImage(named: "checkmark_white"), for: .normal)button.contentVerticalAlignment = .fillbutton.contentHorizontalAlignment = .fillbutton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)