title: Changing Gradient Background layer color for UiView IOS using CAGradientLayer classe
body: <p>i want to change the color of my gradient background when i trigger an action.
  I tryed by many way with no succes.</p>
  
  <p>BackgroundLayer.h</p>
  
  <pre><code>    #import &lt;Foundation/Foundation.h&gt;
      #import &lt;QuartzCore/QuartzCore.h&gt;
  
      @interface BackgroundLayer : NSObject
  
      +(CAGradientLayer*) greyGradient;
      +(CAGradientLayer*) blueGradient;
  
      @end
  </code></pre>
  
  <p>BackgroundLayer.m</p>
  
  <pre><code>@implementation BackgroundLayer
  
  //Metallic grey gradient background
  + (CAGradientLayer*) greyGradient {
  
      UIColor *colorOne = [UIColor colorWithWhite:0.9 alpha:1.0];
      UIColor *colorTwo = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.85 alpha:1.0];
      UIColor *colorThree     = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.7 alpha:1.0];
      UIColor *colorFour = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.4 alpha:1.0];
  
      NSArray *colors =  [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, colorThree.CGColor, colorFour.CGColor, nil];
  
      NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
      NSNumber *stopTwo = [NSNumber numberWithFloat:0.02];
      NSNumber *stopThree     = [NSNumber numberWithFloat:0.99];
      NSNumber *stopFour = [NSNumber numberWithFloat:1.0];
  
      NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, stopThree, stopFour, nil];
      CAGradientLayer *headerLayer = [CAGradientLayer layer];
      headerLayer.colors = colors;
      headerLayer.locations = locations;
  
      return headerLayer;
  
  }
  
  //Blue gradient background
  + (CAGradientLayer*) blueGradient {
  
      UIColor *colorOne = [UIColor colorWithRed:(120/255.0) green:(135/255.0) blue:(150/255.0) alpha:1.0];
      UIColor *colorTwo = [UIColor colorWithRed:(57/255.0)  green:(79/255.0)  blue:(96/255.0)  alpha:1.0];
  
      NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
      NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
      NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];
  
      NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];
  
      CAGradientLayer *headerLayer = [CAGradientLayer layer];
      headerLayer.colors = colors;
      headerLayer.locations = locations;
  
      return headerLayer;
  
  }
  
  @end
  </code></pre>
  
  <p>In my view controller i have a switch on proximity (i work with ibeacon).
  the switch have 4 cases. i want Case 1(Far) with blue gradient. Case 2(Near) with grey gradient. I initialise with the layer in "(void)viewWillAppear:(BOOL)animated" and then i tryed that in the did range delegate :</p>
  
  <pre><code>case CLProximityFar:
  {
  
  [[[self.view.layer sublayers] objectAtIndex:0] removeFromSuperlayer];
  self.bgLayer = [BackgroundLayer blueGradient];
  self.bgLayer.frame = self.view.bounds;
  [self.view.layer insertSublayer:self.bgLayer atIndex:0];
  ....
  </code></pre>
  
  <p>not working.
  i also tryed like this </p>
  
  <pre><code>case CLProximityNear:
  {
  
  [[[self.view.layer sublayers] objectAtIndex:0] removeFromSuperlayer];
  CAGradientLayer *gradient = [[self.view.layer sublayers] firstObject];
  gradient = [BackgroundLayer blueGradient];
  [self.view.layer insertSublayer:gradient atIndex:0];
  [self.view.layer setNeedsDisplay];
  ....
  </code></pre>
  
  <p>Not working.</p>
  
  <p>Anyone can help me ? i need a solution for changing background color when i m on "near" and rechange it back when i m on "far".</p>
  
  <p>Thanks in advance for your help. </p>