3

Tips on Using UIGlassButton to Make Shiny Glass iPhone Buttons

Posted February 19th, 2010 in iPhone by erich

Erica Sadun linked to Jonathan Wight’s nifty use of a private UIGlassButton class to make shiny “glass” iPhone buttons, and here are a few tricks on using it well.

First up, I like making a new Utility Application in XCode for this, because you can plug Jonathan’s code into the showInfo method.  When you run the app in the simulator, just tap the info button to generate the button image.

And as for the button image itself, I recommend setting the height of the image (in CGRectMake) to the exact height of the button you are going to be using, plus 8 pixels due to the empty space around the button image where shaodw live.  If you’re using Interface Builder, check the Size Inspector tab to see what the height of your button is.  Sure, the iPhone SDK will resize your image to fit whatever button size you’re using, but it won’t be pixel perfect.  Also I set the width at 29, as you don’t really need more width than that for making a resizable button image, such as in the following code:

UIImage *blueButtonImage = [[UIImage imageNamed:@"blueButton.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[myButton setBackgroundImage:blueButtonImage forState:UIControlStateNormal];

For more details on the right Interface Builder settings for custom UIButtons (such as to be sure to pick Custom for button type or you’ll end up with an ugly gray outline), check out Christopher McCulloh’s tutorial.

3 Responses so far.

  1. Chris says:

    Hey,

    Thanks for the additional details on top of the tuaw post.
    I can’t seem to get my button quite right.

    The button I want to customize is set in interface builder to be 300x41px, so I create my glossy image to be 29x49px (+8 as you said).
    I use this code to set the image to the button:
    [generateButton setBackgroundImage:[[UIImage imageNamed:@"greenButton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0] forState:UIControlStateNormal];

    This is what I end up with:
    http://img.skitch.com/20100219-gggm5cawkw887x4nftdhnf38dd.png

    A horrid white border. I have tried playing around with the buttons shadow property, but that didn’t seem to change anything. Also tried playing around with the size of the button and resizing the image, but still no juice…
    Any suggestions? :)

  2. erich says:

    You need to make sure your button type is set to Custom in Interface Builder (or in your code) and not the default Rounded Rect. That’s what’s painting the gray border. I had it too :-)

  3. Chris says:

    perfect :) thank you!

Leave a Reply