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 8594357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:10:54+00:00 2026-06-12T00:10:54+00:00

I have a delegate method of a UIVIew and in the drawRect method I

  • 0

I have a delegate method of a UIVIew and in the drawRect method I add in a UIBezierPath to show a shadow on a square.

 //// General Declarations
 CGContextRef context = UIGraphicsGetCurrentContext();

 //// Shadow Declarations
 UIColor* shadow = [UIColor blackColor];
 CGSize shadowOffset = CGSizeMake(0, -0);
 CGFloat shadowBlurRadius = 15;


 //// Rectangle Drawing
 rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(8, 8, 44, 44)];
 CGContextSaveGState(context);
 CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor);
 [[UIColor whiteColor] setFill];
 [rectanglePath fill];
 CGContextRestoreGState(context);

I then want to change the colour of the shadow based on certain criteria so I mave a method called makeRed.

- (void)makeRed {
  NSLog(@"makeRed");
  CGContextRef context = UIGraphicsGetCurrentContext();
  // Shadow Declarations
  UIColor* shadow = [UIColor redColor];
  CGSize shadowOffset = CGSizeMake(0, -0);
  CGFloat shadowBlurRadius = 15;
  CGContextSaveGState(context);
  CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow.CGColor);
  [[UIColor whiteColor] setFill];
  [rectanglePath fill];
  CGContextRestoreGState(context);
}

But when I call the method I get the message:

: CGContextSaveGState: invalid context 0x0

any ideas how I can either set the right context or change the shadow colour in a different way?

Note the initial drawing of the shadow works perfectly, as there are other attributes to the delegate i.e. some fancy animations using the .layer method of creating shadows wont work.

Cheers

  • 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-12T00:10:55+00:00Added an answer on June 12, 2026 at 12:10 am

    In the UIView docs you can see drawRect:

    By the time this method is called, UIKit has configured the drawing environment appropriately for your view and you can simply call whatever drawing methods and functions you need to render your content.

    So the drawing you do inside drawRect: is correct as the drawing context is setup correctly etc, this is not the case in your makeRed method.

    I would suggest having an ivar shadowColor and then use this inside your drawRect: method.
    Your makeRed would then look like this

    - (void)makeRed;
    {
      self.shadowColor = [UIColor redColor];
      [self setNeedsDisplay];
    }
    

    and then modify the line in drawRect: to

    CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, self.shadowColor.CGColor);
    

    setNeedsDisplay is used to tell UIKit that you would like your view to be redrawn, which then results in drawRect: being called again.

    You will of course have to initialize _shadowColor = [UIColor blackColor] in the init* method.

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

Sidebar

Related Questions

Eg. I have following delegate method I want to use as a callback function
Is it possible to implement Delegate methods for FileSystemWatcher? I have a delegate method
I have the following delegate method which is listening for when text changes in
I have a UISearchbar with delegate method - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if(searchText.length>3){
I have a method that accepts a generic delegate as a parameter, and inserts
I have this method in App Delegate that makes a window and content view
I have my main application delegate which contains a method that returns an object.
I have the following code (calling a method returning IEnumerable): FibonacciWithLinq.Skip(delegate() { return 5;
I have the following animation at the end of my cellForRowAtIndex Delegate method which
I have a method like the following: - (void)add:(id)sender { MyAddViewController *controller = nil;

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.