Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tint a UIButton?

Tags:

iphone

I'm in the process of adding custom buttons to my iPhone UI and want to make them have the glassy look from Apple's apps. I have a good default glass image, but I'd hate to have to have a separate image for each tint I want (red, green, blue, etc.).

Is there a way to load a grayscale PNG and adjust it to the color I want? Better yet, is there a way to get Apple's glassy look without having to load custom images at all?

like image 507
drewh Avatar asked Sep 23 '08 15:09

drewh


3 Answers

A little late, but in case somebody like me searches for this kind of information: use a UISegmentedControl with a single button, set it as momentary, and set its tintColor. This way, no need to prepare as many PNGs as you have colors, and the framework takes care of all the rendering for you.

Example:

UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Cancel"]];
[cancelButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[cancelButton setTintColor:[UIColor colorWithRed:0.8 green:0.3 blue:0.3 alpha:1.0]];
[cancelButton setMomentary:YES];
[cancelButton addTarget:self action:@selector(didTapCancel:) forControlEvents:UIControlEventValueChanged];
[self addSubview:cancelButton];
[cancelButton release];
like image 173
Cyrille Avatar answered Oct 25 '22 15:10

Cyrille


There's a sample at the always excellent "Cocoa With Love" site: Drawing gloss gradients in CoreGraphic

like image 6
Alfons Avatar answered Oct 25 '22 13:10

Alfons


There isn't a one-liner way that I know of, but you might be able to get the desired effect by taking the default grayscale image and compositing it with (i.e. drawing it on top of) a solid color. If you look through the Core Graphics documentation, you can see there are over a dozen different compositing methods (e.g., Color Burn), and some combination of them may produce the effect you want.

like image 2
benzado Avatar answered Oct 25 '22 13:10

benzado