Gradient drawing on UIView does not work with iOS 9

I am trying to draw a gradient in the background View Viewer with iOS 9, but I do not know why, does not work

Here is my method that is called from viewDidLoad

- (void)drawGradient
{
  CAGradientLayer *gradient = [CAGradientLayer layer];
  gradient.frame = self.view.bounds;

  gradient.colors = @[[UIColor greenColor], [UIColor redColor]];
  gradient.locations = @[@0.0, @1.0];

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

But nothing happens, and the gradient does not appear. What am I doing wrong?

+4
source share
1 answer

Thanks to Larma's comment, I realized my mistake.

Instead

gradient.colors = @[[UIColor greenColor], [UIColor redColor]];

right it

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

Because it gradient.colorsexpects a NSArrayof CGColorRef. Casting is (id)also necessary to create NSArray.

+11
source

Source: https://habr.com/ru/post/1609600/


All Articles