I have a button with a)Image and b)Background Image - all built in a storyboard. The image is popping out of button as background image has a different shape. I want to give the actual image (a) some padding from all sides so it can shrink back within the background image. I can't do it with insets as they only give padding to non-opposite sides, when same insets are set to opposite sides - image doesn't shrink. It would be great to know how it's done in storyboard and programmatically.
You should set the button to fill the image. Doing so lets you use the image insets as padding. Note that this will stretch the image, so you need to pad properly on left/right vs top/bottom if you want a correct aspect ratio right.
In Storyboard you do this in the buttons control section. Then adjust the button's image insets.
In code you do like this.
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With