Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8043479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:54:47+00:00 2026-06-05T04:54:47+00:00

I am trying to add a CAGradientLayer in my drawRect, the code is the

  • 0

I am trying to add a CAGradientLayer in my drawRect, the code is the following:

- (void)drawRect:(CGRect)rect {

    CAGradientLayer *gradientOverlay = [CAGradientLayer layer];
    CGColorRef grayColor = [UIColor colorWithRed:37/255.f green:37/255.f 
                                            blue:37/255.f alpha:1.0].CGColor; 
    CGColorRef blueColor = [UIColor colorWithRed:23.0/255.0 green:171.0/255.0 
                                            blue:219.0/255.0 alpha:1.0].CGColor;
    gradientOverlay.colors = [NSArray arrayWithObjects:
                              (id) grayColor,
                              (id) grayColor,
                              (id) blueColor,
                              nil];
    gradientOverlay.locations = [NSArray arrayWithObjects:
                                 [NSNumber numberWithFloat:0],
                                 [NSNumber numberWithFloat:0.4],
                                 [NSNumber numberWithFloat:1],
                                 nil];

    CGPoint startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));

    gradientOverlay.startPoint = startPoint;
    gradientOverlay.frame = self.bounds;
    gradientOverlay.endPoint = endPoint;

    self.layer.mask = gradientOverlay;

}

Any idea why this is not working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:54:48+00:00Added an answer on June 5, 2026 at 4:54 am

    Add your CAGradientLayer as a sublayer of your self.layer.

    Note: If you’re doing it in the drawRect: method make sure you’re not adding a new CAGradientLayer every time drawRect: is called.

    UPDATE (regarding the comment):
    Here’s the code for what you’re asking:

        //Create a layer that holds your background image and add it as sublayer of your self.layer
        CALayer *layer = [CALayer layer];
        layer.frame = self.layer.frame;
        layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;
        [self.layer addSublayer:layer];
    
    //Create your CAGradientLayer
        CAGradientLayer *gradientOverlay = [CAGradientLayer layer];
        CGColorRef grayColor = [UIColor colorWithRed:37/255.f green:37/255.f 
                                                blue:37/255.f alpha:1.0].CGColor; 
        CGColorRef blueColor = [UIColor colorWithRed:23.0/255.0 green:171.0/255.0 
                                                blue:219.0/255.0 alpha:1.0].CGColor;
        gradientOverlay.colors = [NSArray arrayWithObjects:
                                  (id) grayColor,
                                  (id) grayColor,
                                  (id) blueColor,
                                  nil];
        gradientOverlay.locations = [NSArray arrayWithObjects:
                                     [NSNumber numberWithFloat:0],
                                     [NSNumber numberWithFloat:0.4],
                                     [NSNumber numberWithFloat:1],
                                     nil];
    
        CGPoint startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
        CGPoint endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));
    
        gradientOverlay.startPoint = startPoint;
        gradientOverlay.frame = self.layer.frame;
        gradientOverlay.endPoint = endPoint;
    //set its opacity from 0 ~ 1
        gradientOverlay.opacity = 0.6f;
    //add it as sublayer of self.layer (it will be over the layer with the background image
        [self.layer addSublayer:gradientOverlay];
    

    Note: You don’t have to do this in drawRect: method. Create your layer hierarchy in the init method for example, if you need to change the geometry of some views/layers override the layoutSubviews method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to add a css class in my javascript code but when i
when trying to add a project to my WebSphere, I get the following error:
I'm trying add some code at run time to the head element. When I
I am trying add OR operator in awk command. The following does not work.
Trying to add a 'box' to a form at design time, I looked up
Trying to add email notification to my app in the cleanest way possible. When
Trying to add tests to the gobject-introspection test suite to cover more of the
Trying to add a class object into a List using reflection, but when invoking
Trying to add a dynamic class value to one of my columns in CGridView:
Im trying to add an element to a database and then return a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.