Skip to content

Instantly share code, notes, and snippets.

@shuizhongyueming
Created April 7, 2014 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuizhongyueming/10016956 to your computer and use it in GitHub Desktop.
Save shuizhongyueming/10016956 to your computer and use it in GitHub Desktop.
一个通用的给UIButton的background的图片做拉伸的委托
@implementation UIButton (StretchableUIButton)
/* Automatically set cap insets for the background image. This assumes that
the image is a standard slice size with a 1 px stretchable interior */
- (void)setBackgroundImageStretchableForState:(UIControlState)controlState
{
UIImage *image = [self backgroundImageForState:controlState];
if (image)
{
CGFloat capWidth = floorf(image.size.width / 2);
CGFloat capHeight = floorf(image.size.height / 2);
UIImage *capImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(capHeight, capWidth, capHeight, capWidth)];
[self setBackgroundImage:capImage forState:controlState];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment