Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Monday, April 13, 2009

Custom buttons for your Objective-C applications

Monday, April 13, 2009   

You have a few choices (that I know of right now) for making custom versions of your buttons for an Objective-C project in InterfaceBuilder (iPhone). You can turn your UIButton into a custom type, and use a background image... however you're locked into making the background match the size of your button.

In Flash the option of using a Scale-9 approach is available and it's here for Objective-C too. This method will allow you to make very small base graphics to serve as your button background and it will resize properly. The only drawback is that you'll need to register each button to affect it. The other option would be to subclass UIButton and then make sure you use it instead. I haven't had the need to do that, nor have I even really tried that.

To see the Obj-C skinning in action, here is the code I am using from a test project that I have (you can see what happens as well):

- (void)viewDidLoad {
UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:10];
[button1 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[button2 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[button3 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[button4 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[button5 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[button6 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];

UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:10];
[button1 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[button2 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[button3 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[button4 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[button5 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[button6 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[super viewDidLoad];
}

Labels: ,

 
 Return to the main page
Comments:

There are currently 0 Comments:

 Leave a comment