Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

iphone

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?

like image 749
MusiGenesis Avatar asked Nov 01 '11 20:11

MusiGenesis


1 Answers

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;
like image 79
PTBG Avatar answered Sep 28 '22 03:09

PTBG