iosuibuttonuiimagesetneedsdisplay

Resizing a UIButton after setting a new image


I have a UIButton subclass that I animate whenever the user presses it. When it hasn't been pressed, the UIButton uses a "non-selected" image and looks great, but when the user presses it I set a new "selected" image.

This works great except the selected image is bigger than the non-selected image, so when the button sets the selected image, it does not resize to fit the new image.

The button is too small and the image is pinched in and looks distorted.

I tried calling some methods like setNeedsLayout and setNeedsDisplay (separately, not together) after setting the new image but the button stays the same size.

How can I get the button to resize whenever I set a new image on it?


Solution

  • You can set the button.frame to the size of the image.