Skip to content

Instantly share code, notes, and snippets.

@cybersamx
Last active August 29, 2015 14:04
Show Gist options
  • Save cybersamx/729fc680f724063435ba to your computer and use it in GitHub Desktop.
Save cybersamx/729fc680f724063435ba to your computer and use it in GitHub Desktop.
Create a custom UIButton with a custom background image and stretchable width.
// 1. Create a PNG background image.
// 2. Use UIEdgeInsets to stretch the image eg. 8.0f left and right of rect to remain unstretched
// 3. Make sure the UIButton height is the same as the image height
UIImage *image = [UIImage imageNamed:@"button_background_blue.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(0.0f, 8.0f, 0.0f, 8.0f);
UIImage *stretchedImage = [image resizableImageWithCapInsets:insets];
[self.button setBackgroundImage:stretchedImage forState:UIControlStateNormal];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment