Skip to content

Instantly share code, notes, and snippets.

@dtrauger
Last active June 24, 2020 14:45
Show Gist options
  • Save dtrauger/5722782 to your computer and use it in GitHub Desktop.
Save dtrauger/5722782 to your computer and use it in GitHub Desktop.
Create Gradient Background for View in Objective C
// Create the colors
UIColor *topColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0];
UIColor *bottomColor = [UIColor colorWithRed:56.0/255.0 green:56.0/255.0 blue:56.0/255.0 alpha:1.0];
// Create the gradient
CAGradientLayer *theViewGradient = [CAGradientLayer layer];
theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor, (id)bottomColor.CGColor, nil];
theViewGradient.frame = theView.bounds;
//Add gradient to view
[theView.layer insertSublayer:theViewGradient atIndex:0];
@goel232
Copy link

goel232 commented Feb 27, 2017

background Gradient : rgb(118,118,118) | #ffffff | rgb(198,198,197) linear left to right

Hello Sir how can i set my gradient to as required by the client please suggest ??
I have tried this way but it is happening in Vertical direction I want it in Horizontal way
UIColor *leftColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0];
UIColor *middleColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
UIColor *rightColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:197.0/255.0 alpha:1.0];

// Create the gradient
CAGradientLayer *theViewGradient = [CAGradientLayer layer];
theViewGradient.colors = [NSArray arrayWithObjects: (id)leftColor.CGColor, (id)middleColor.CGColor,(id)rightColor.CGColor, nil];
theViewGradient.frame = self.view.bounds;

//Add gradient to view
[self.view.layer insertSublayer:theViewGradient atIndex:0];

@radorbit
Copy link

@goel232 use "startPoint" and "endPoint" properties on CAGradientLayer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment