Search
Rich's Mad Rants
Powered by Squarespace

Creating iOS 5 Apps Discussion > Page 198 - another option to create custom buttons

Great opportunity to learn about Core Animation to create a custom button. Just thought I'd mention a couple other options I've used...

// Copy this to UnitSelectorViewController.m viewDidLoad...
// Option 1: Use ButtonMaker (https://github.com/dermdaly/ButtonMaker) to create a 29 x 46 button in your choice of color and import button.png and button-highlight.png
// Option 2: Download assets from http://developer.apple.com/library/ios/samplecode/uicatalog/UICatalog.zip and import whiteButton.png and blueButton.png (change button.png to whiteButton.png, and button-highlight to blueButton.png)
UIImage *buttonImageNormal = [UIImage imageNamed:@"button.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[self.doneButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"button-highlight.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[self.doneButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];

You'll also need to set the background color of the button to Default in the storyboard.

December 26, 2011 | Unregistered CommenterScott

These are all good alternatives.

I'd highly recommend using stretchable images whenever possible, just to cut down on memory usage. Note, however, that the stretchableImageWithLeftCapWidth:topCapHeight: is deprecated. You should use resizableImageWithCapInsets: instead. I believe I cover this (briefly) in Chapter 8.

December 27, 2011 | Registered CommenterRichard Warren