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,694
  • 10
  • 59
  • 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,304
  • 4
  • 41
  • 45
1

gradient.colors need to be CGColor, not UIColor.

try

NSArray *gradientColors = [[NSArray alloc] initWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

Use CGColor instead of UIColor:

    NSArray *gradientColors = [[NSArray alloc] initWithObjects:topColor.CGColor, 
bottomColor.CGColor, nil];
Eugene
  • 10,006
  • 4
  • 37
  • 55