IOS CAGradientLayer does not work with certain colors

advertisements

I have used below code to set gradient background color of my view.

 CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = @[(id)[UIColor colorWithRed:44/255 green:58/255 blue:76/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255 green:22/255 blue:30/255 alpha:1].CGColor];
    [self.view.layer insertSublayer:gradient atIndex:0];

But its not working. Screen will appear with black color. But when I try to set color like below

gradient.colors = @[(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor];

then it working fine.I am getting correct output. Can anyone help me to fine why gradient color is not working with UIColor colorWithRed function.

Thanks in advance.


Try this:

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:202.0/255.0 green:107.0/255.0 blue:122.0/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:192.0/255.0 green:144.0/255.0 blue:120.0/255.0 alpha:1] CGColor], nil];
gradient.frame = self.view.bounds;
[self.view.layer insertSublayer:gradient atIndex:0];