Home > DeveloperSection > Forums > How to use Gradient color in UIView in iOS?
Tarun Kumar

Total Post:210

Posted on    December-30-2015 6:57 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 324  View(s)
Rate this:
I want to create gradient color and want to use it into our UIView. 
Can anyone tell me how can I generate a view with gradient color background in runtime.
If is there any way to do that then please provide me the code also.

Glen Martin
Glen Martin

Total Post:27

Posted on    December-30-2015 10:57 PM

The code I am providing here is already running in my own application, so I hope my code is helpful for you:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = selectedView.bounds;
gradient.colors = [NSArray arrayWithObjects:
    (id)[[UIColor blackColor] CGColor],
    (id)[[UIColor greenColor] CGColor],
    (id)[[UIColor redColor] CGColor], nil];

our gradient color is created, now we assign it in our view layer, like this:

[view.layer insertSublayer:gradient atIndex:0];

Modified On Dec-30-2015 10:58:13 PM

Don't want to miss updates? Please click the below button!

Follow MindStick