Creating a Custom UIButton

cocoa-touch, uibutton, uikit

Solution

First create a stretchable `UIImage` (this assumes the 'cap' at the end of your button image is 10px):

UIImage *backgroundImage = [[UIImage imageNamed:@"ImageBackground.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];

Then use the stretchable image as the background:

[myButton setBackgroundImage:backgroundImage forState:UIControlStateNormal];

When you set the backgroundImage on a `UIButton` for `UIControlStateNormal`, it acts as the background for all states, unless you explicitly set a different backgroundImage for any of the other states.

Problem

I am looking to create a Custom UIButton (not subclass) progrmatically, I would like it to have a background image that will stretch as the UIButton's width increases. How would I do this?

Original source