Gian
Gian

Reputation: 1213

Button image stretching

I am having a problem with the button image. I have a green background and an icon. The problem is that on larger screens the image stretches due to the button also stretch to the size of the screen.

I found several other topics they were talking about something, but none of them helped me solve this problem.

The next best picture explains the problem: enter image description here

Upvotes: 1

Views: 393

Answers (3)

Shamas S
Shamas S

Reputation: 7549

If you want your button to look like the 3rd image, you will have to use an image of that size and resolution.

Alternatively, you can set contentMode on your UIButton's imageView object. Use this line of code in your viewDidLoad function

button.imageView!.contentMode = UIViewContentMode.ScaleAspectFit

When your constraints increase the size of your button, the image used inside it will abide by the contentMode property set. Using those you can stretch it, which it's doing right now, or you can keep the aspect ratio, in which case it will show empty space on sides.

Upvotes: 1

rmd2
rmd2

Reputation: 157

It look like your constraints doesn't take on consideration the size of the asset.

You should set a Width and a Heigth constraints to your image asset.

Upvotes: 0

Alexander
Alexander

Reputation: 8147

Play around with the content mode settings, try 'Aspect fit' instead of the currently selected.

Upvotes: 0

Related Questions