iPhone, iPod touch, iPad tutorials, examples and sample code. Come and get it!

Friday, July 30, 2010

UIColor macro with hex values

Cocoa has several colors built into the UIColor class. For example:
[UIColor redColor];
[UIColor darkGrayColor];

This is great, but what if you want one of the thousands of colors not found in the pre-defined list? There's an easy way to do that:
[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];

Perfect. But what if you just have a list of hex color codes? Wouldn't it be nice if you didn't have to convert each of the color components in the hex code to its corresponding RGB decimal value? Here's where a simple and very useful macro comes into play. Just place the code below in a header file and you're done. (They're identical except that UIColorFromRGB always sets the alpha value to 1.0, whereas UIColorFromRGBWithAlpha allows you to set the alpha value.)

Labels: , , ,

Creating UIViews programmatically

If you want to create a new UIViewController object without using a Nib file in Interface Builder, you must use the loadView method. It's rather simple, but key to doing things programmatically.
- (void)loadView {
 CGRect viewFrame;
 viewFrame = CGRectMake(0, 0, MASTER_WIDTH, MASTER_HEIGHT);
 self.view = [[[UIView alloc] initWithFrame:viewFrame] autorelease];
(Note: MASTER_WIDTH and MASTER_HEIGHT are simply constants defining the width and height of the screen.)

Once your view is created, you are free to implement, as usual, viewDidLoad, viewWillAppear, viewDidAppear, etc.

Labels: , , ,

« Older Entries  
Newer Entries »