Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton padding inside Image from all sides

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.

like image 320
Curtis Avatar asked Feb 17 '18 21:02

Curtis


1 Answers

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.

enter image description here

In code you do like this.

button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
like image 91
LGP Avatar answered Sep 23 '22 18:09

LGP