I have a UIImage resizing perfectly using resizableImageWithCapInsets to make a badge containing a text label. I’m interested to know is there a way to do this in a storyboard, or in a view created with a nib?
There’s no technical reason to require this. Most of the UI is done in the nib and I would like to keep as much there as possible rather than loading the image, making it resizable, finding the text dimensions, resizing the UIImageView and then applying the label in code.
I can’t make this project iOS6-only yet (deployment target ios5+) so using layout constraints isn’t an option.
Yes, there is.
The trick is to override
-initWithCoder:and the image setters (setImage:forState:andsetBackgroundImage:forState:) to create the resizable versions of the same image you set in the xib. Obviously, they still won’t resize properly in xib itself, but it will be resized properly after initialization. Don’t forget to setNAResizableButtonas the custom class ofUIButton.Sure beats doing it programmatically. 🙂
Interface:
Implementation: