Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement scale-9 (9-slice) for custom background art created for UIButton

Tags:

ios

uikit

What do I need to do programatically in order to use the same background image for UIButton of variable size? (commonly known as 9-slice scaling or scale-9)

like image 367
Boon Avatar asked Jul 23 '09 15:07

Boon


2 Answers

I know this is an old thread, but to anyone who stumbles upon this after iOS 5 is released, it should be noted from Apple's documentation that the stretchableImageWithLeftCapWidth:topCapHeight: method is deprecated as of iOS 5:

Deprecated UIImage Methods

Deprecated in iOS 5.0. Deprecated. Use the resizableImageWithCapInsets: instead, specifying cap insets such that the interior is a 1x1 area.

So now that iOS 6 has been announced and will be here soon, anyone developing for iOS 5 and higher should probably take a look at resizableImageWithCapInsets instead, which documentation can be found here:

UIImage Class Reference - resizableImageWithCapInsets

Just thought I'd mention it to help any developers who needed an updated answer to this problem.

like image 82
Evan K. Stone Avatar answered Oct 23 '22 18:10

Evan K. Stone


Check out:

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

In the UIImage class.

If I understand what you're looking for correctly, this while allow you to use one png to define how your button looks, and it will stretch to any size vertically or horizontally.

like image 39
Nathaniel Martin Avatar answered Oct 23 '22 18:10

Nathaniel Martin