iPhone: Create a Gradient View

// parentView is any view, upon which you want to apply the gradient effect 
UIView *parentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)] autorelease];

// gradientLayer 
CAGradientLayer *gradientLayer = [CAGradientLayer layer];

// gradientLayer frame is same as parentView 
gradientLayer.frame = parentView.frame;

// apply gradient Color. we are creating an array of 2 elements 
// 1st element: starting color of the gradient 
// 2nd element: ending color of the gradient 
gradientLayer.colors = [NSArray arrayWithObjects:(id) [[UIColor clearColor] CGColor], (id) [[UIColor colorWithRed:130 green:191 blue:71 alpha:1.0] CGColor], nil];

// add the gradient layer to your parent view 
[parentView.layer insertSublayer:gradientLayer atIndex:0];

// some cases we don't want any user interaction (touch event, etc.) on the view 
[parentView setUserInteractionEnabled:false];

// add the parent view on your view controller
[self.view addSubview:parentView];


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s