Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image size in UIButton?

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?

like image 715
Ivan Kisin Avatar asked Jul 03 '18 07:07

Ivan Kisin


2 Answers

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)

like image 197
Sanket Ray Avatar answered Nov 15 '22 21:11

Sanket Ray


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.

like image 44
AlrightyRob Avatar answered Nov 15 '22 20:11

AlrightyRob