0

I am trying to create a gradient using some custom UIcolors I created but all that shows up is white when I run the app. I created the colours in app delegate so I could use them through out the app and this is the code I used for them:

//AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
+ (UIColor*)blueColor1;
+ (UIColor*)blueColor2;
+ (UIColor*)yellowColor1;

@property (strong, nonatomic) UIWindow *window;

@end

//AppDelegate.m

+ (UIColor*)blueColor1 {
    return [UIColor colorWithRed:112.0 / 255.0 green:184.0 / 255.0 blue:255.0 / 255.0 alpha:1.0];
}
+ (UIColor*)blueColor2 {
    return [UIColor colorWithRed:190.0 / 255.0 green:243.0 / 255.0 blue:250.0 / 255.0 alpha:1.0];
}
+ (UIColor*)yellowColor1 {
    return [UIColor colorWithRed:245.0 / 255.0 green:243.0 / 255.0 blue:204.0 / 255.0 alpha:1.0];
}

And for the gradient itself I used this:

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[AppDelegate blueColor1], (id)[AppDelegate blueColor2], (id)[AppDelegate yellowColor1], nil];
    [self.view.layer insertSublayer:gradient atIndex:0];

What do I need to do to get the gradient to show?

  • You really want to use Objective-C collection literals: instead of `[NSArray arrayWithObjects: this, that, nil]`, use `@[this, that]`. – jcaron Jan 25 '16 at 23:18
  • Possible duplicate of [CAGradientLayer not working](http://stackoverflow.com/questions/33060648/cagradientlayer-not-working) – Larme Jan 26 '16 at 10:59

1 Answers1

1

From the documentation, the colors property is:

An array of CGColorRef objects defining the color of each gradient stop. Animatable.

Therefore you want:

gradient.colors = @[(id)[AppDelegate blueColor1].CGColor, (id)[AppDelegate blueColor2].CGColor, (id)[AppDelegate yellowColor1].CGColor];
Hamish
  • 78,605
  • 19
  • 187
  • 280