UIButton padding inside Image from all sides UIButton padding inside Image from all sides swift swift

UIButton padding inside Image from all sides


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 = .fillbutton.contentHorizontalAlignment = .fillbutton.imageEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)