How to set image size in UIButton?

There may be two options to fix the problem.

  1. Set the content mode to .scaleAspectFit and the image should not go out of the bounds : myButton.imageView?.contentMode = .scaleAspectFit

  2. Adjust the image insets: myLikesButton.imageEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right)


Sanket was close:

  1. Adjust the image insets: Button.imageEdgeInsets = UIEdgeInsetsMake(40, 40, 40, 40)

**The (40, 40, 40, 40) is just an example button sizing and you can adjust it to your satisfaction. Like Sanket answered (Top, Left, Bottom, Right) is what you should use, just adjust with actual numbers.


iOS 15

  let button = UIButton()
  var config = UIButton.Configuration.filled()
  config.imagePadding = 40
  button.configuration = config
  return button