MusiGenesis
MusiGenesis

Reputation: 75296

How to add an image to a UIButton so that it isn't stretched to fill the button?

I have a UIButton, and I'm adding an image to it using this code:

[btnCall setImage:[UIImage imageNamed:@"phoneicon"] forState:UIControlStateNormal];

The problem is that this image is being stretched to fill the button, and I don't want it to be stretched at all - I want it to retain its "natural" size and be positioned in the center of the button, even when the button is resized.

My searching on this topic shows a bunch of people with the opposite problem: they're setting the image and they want it to be stretched to fill the button. What am I doing wrong here?

Upvotes: 5

Views: 3772

Answers (1)

PTBG
PTBG

Reputation: 595

I think this is because the default content mode is set to 'UIViewContentModeScaleToFill' so you need to set it to 'UIViewContentModeCenter'

btnCall.imageView.contentMode = UIViewContentModeCenter;

Upvotes: 9

Related Questions