UIButton won't go to Aspect Fit in iPhone

I have a couple UIButtons, and in IB they're set to Aspect Fit, but for some reason they're always stretching. Is there something else you have to set? I tried all the different view modes and none of them work, they all stretch.


The solution is to set the contentMode on the imageView property of the UIButton. The UIButton has to be created with custom type for this to work I believe (otherwise nil is returned for this property).


This method worked for me very well.:

In Xib select the button and set user defined runtime attributes:

  • Key path: self.imageView.contentMode
  • Type: Number
  • Value: 1

Click here to see more clearly the solution

We use number because you cant use enum there. But the UIViewContentModeScaleAspectFit is equal to 1.


I've had that problem before. I solved it by putting my image in a UIImageView, where contentMode settings actually work, and putting a transparent custom UIButton over top of that.

EDIT: This answer is obsolete. See @Werner Altewischer's answer for the correct answer in modern versions of iOS.


If you are doing this in Interface Builder, you can use the Runtime attributes inspector to set this directly without any code.

Set your Key Path on the button to be "imageView.contentMode" with a type of "Number" and a value of "1" (or whichever mode you would like).

imageview.contentMode = 1