0

I am using CAGradientLayer to create a background layer, as described in this answer to this question: Gradients on UIView and UILabels On iPhone

However when I use this code I get a exc_bad_access error with a reference to CGColorSpaceGetModel.

UILabel *headerText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width -10, 18)];    

CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.bounds = headerText.bounds;

UIColor *topColor = [[UIColor alloc] initWithRed:0.5647 green:0.6235 blue:0.6667 alpha:1.0];

UIColor *bottomColor = [[UIColor alloc] initWithRed:0.7216 green:0.7569 blue:0.7843 alpha:1.0];

NSArray *gradientColors = [[NSArray alloc] initWithObjects:topColor, bottomColor, nil];

gradient.colors = gradientColors;

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

Any idea what could be causing this error?

Community
  • 1
  • 1
Andrew Lauer Barinov
  • 5,614
  • 9
  • 57
  • 83

3 Answers3

6

You need to use CGColorRefs not UIColor... there is a property on UIColor to get the CGColorRef....

NSArray *gradientColors = [[NSArray alloc] initWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
Simon Lee
  • 22,274
  • 4
  • 39
  • 44
1

gradient.colors need to be CGColor, not UIColor.

try

NSArray *gradientColors = [[NSArray alloc] initWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
vikingosegundo
  • 51,574
  • 14
  • 135
  • 174
0

Use CGColor instead of UIColor:

    NSArray *gradientColors = [[NSArray alloc] initWithObjects:topColor.CGColor, 
bottomColor.CGColor, nil];
Eugene
  • 9,868
  • 4
  • 33
  • 54