14

I'm trying to make a clone of decimal pad by stack views and UIButtons. But when I try to make "Backspace" button image inside of it scales to the bounds of button.

How could I make image scale smaller than button itself?

Ivan Kisin
  • 385
  • 1
  • 3
  • 12

4 Answers4

15

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)

Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
Sanket Ray
  • 1,071
  • 1
  • 15
  • 24
  • 2
    Unfortunately this no longer applies -- at least it doesn't work for iOS 13-15. –  Jun 25 '22 at 18:00
3

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.

AlrightyRob
  • 163
  • 13
3

iOS 15

  let button = UIButton()
  var config = UIButton.Configuration.filled()
  config.imagePadding = 40
  button.configuration = config
  return button
Visual Sharp
  • 3,093
  • 1
  • 18
  • 29
0

Things you should keep in mind while loading image to the 'UIBUTTON'

  • Try to use image size of 24*24 image when you are loading images to UIBARBUTTONIMAGE.
  • When loading image in normal 'UIBUTTON' the image should be compatible with the button.
  • Try the button with standard image size which is 32*32.
  • In the right interface builder in storyboard try with Aspect fit.