71

I'm trying to create a custom UITableViewCell programmatically and one of the subviews of this cell is going to be a button with an image in it (a simple image of a magnifying glass). However, I want the button's image to be centered and scaled proportionately down to fit in the button and NOT to be stretched to fill the entire button. Below is my code where self refers to the custom UITableViewCell which I am placing the button into.

self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton setBackgroundImage:[UIImage imageNamed: @image_name_here"] forState:UIControlStateNormal];
self.myButton.frame = CGRectMake(...//something, something)
self.myButton.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.mySearchHelpButton];

Right now the image stretches to fill the entire button rather than scaling proportionately so that it fits nicely.

I have also tried setting the contentMode to UIViewContentModeScaleAspectFill but this doesn't seem to change anything. In fact, none of the different contentModes seem to change anything.

7 Answers 7

68

Have you tried setImage instead of setBackgroundImage?

Sign up to request clarification or add additional context in comments.

Comments

59

Make sure the button is a Custom UIButton

Just using setImage: did not work for me on iOS9.

But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;

2 Comments

Thanks! This works for me too, seems to me this attribute is not exposed in IB, so that we have to manually set it in code instead of from IB. On IB, there is only a content mode setting for the button itself, instead of its imageView. Correct me if I am wrong.
You're spot on RainCast - the only way to set aspect fit for just the imageView seems to be programmatically.
46

In Swift...

button.imageView?.contentMode = . scaleAspectFit

2 Comments

this is now button.imageView?.contentMode = .scaleAspectFit
keep in mind if yourButton.translatesAutoresizingMaskIntoConstraints = falsethen .scaleAspectFit will not be applied
39

Storyboard

Property image.

You must define specific property in Runtime Attributes of Identity inspectorimageView.contentMode, where you set value relatively to rawValue position of enum UIViewContentMode. 1 means scaleAspectFit.

Set button.imageView.contentMode in Storyboard

And button's alignment, in Attributes inspector:

Full alignment of button

9 Comments

thank you, your guide helps me a lot. is it possible for you to give me a brief explanation about what are you doing exactly? thank you very much
@nedaDerakhshesh could you be more specific? Thanks.
This is the rawValue of the scaleAspectFit enum (case scaleAspectFit = 1)
This worked for me, but ONLY after I had unloaded and reloaded the image - ie delete the image name from the image / background image field, then add it again to reload.
You saved my day!
|
10

Swift 4.2

myButton.imageView!.contentMode = .scaleAspectFit

Swift earlier version

myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit

Comments

9

Swift 4.X and 5.X

button.imageView!.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill

Comments

3

I think you guys are missing the obvious issue. The image is too large for the button and iOS then has to guess how you want to scale it. Make sure your images are the right size and you won't have this issue. Of course this is for static images and buttons that you know the size for up-front -- not dynamic content.

2 Comments

What would be the right size for a normal button? less than the width and height of the button itself? or how it is calculated?
@tyoc213, the size should be exactly the point size of the button, width and height, that you want. For example if you want a 44pt x 100pt button, the image asset should be 44x100 pixels @ 1x, 88x200 pixels @ 2x, 132x300 pixels @ 3x.That way auto layout knows can use the image as the intrinsic size of the button.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.