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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:02:24+00:00 2026-05-30T06:02:24+00:00

I have subclassed a UIView and I have a drawRect implemented. I basically want

  • 0

I have subclassed a UIView and I have a drawRect implemented. I basically want this drawRect to be called after I’ve set the CGPoint, which is a property of this UIView. How do I do this? Here’s my code:

-(id) initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder]) {
        point_.x = 0;
        self.page_ = [UIImage imageNamed:@"pages"];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    if (point_.x != 0){
        [self.page_ drawInRect:rect];
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextSetLineWidth(context, 1.0f);
        CGContextMoveToPoint(context, 40, point_.y); 
        CGContextAddLineToPoint(context, 420, point_.y);
        CGContextStrokePath(context);
    }
}

When I’ve set the point of this UIView and I call drawRect in my other UIViewController like follows:

 self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
    [self.page_ drawRect:self.page_.frame];

It gives me all of these errors:

  <Error>: CGContextSaveGState: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetBlendMode: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetAlpha: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextTranslateCTM: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextScaleCTM: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextDrawImage: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextRestoreGState: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextSetLineWidth: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextMoveToPoint: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextAddLineToPoint: invalid context 0x0
    Feb 22 20:24:38 <Error>: CGContextDrawPath: invalid context 0x0

Why is this?

  • 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-05-30T06:02:25+00:00Added an answer on May 30, 2026 at 6:02 am

    You should never, ever call -drawRect: directly.

    Use -setNeedsDisplay instead:

    self.page_.point_ = CGPointMake(-100, self.title_.frameHeight + self.title_.frameX + 40);
    [self.page_ setNeedsDisplay];
    

    If you need to customize the drawing area (aka the CGRect that will be passed to -drawRect:), you can use -setNeedsDisplayInRect:

    As @rob mayoff has suggested, you can override the setter for the point_ property:

    - (void)setPoint_:(CGPoint)point {
       if(!CGPointEqualToPoint(point_, point)) {
          point_ = point;
          [self setNeedsDisplay];
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this UIView that I subclassed and implemented my own drawRect method. When
I have subclassed UIView and provided my own drawRect, which works fine. However I
I have a UIView subclass that draws itself when -drawRect: is called. It only
I have subclassed UIView object inside a uiscrollview which displays a pdf page. I
I have a subclass of UIView in which it has a drawRect in it.
I have a class that subclasses UIView called DrawView. This class contains custom drawing
I have subclassed UIView so I can draw some lines in it. In this
I have a UIView subclass and I want the keyboard to appear when it
I have a UIView subclass ( CustomView for purposes of this question) that has
I have a subclass s of UIView. I want to put some buttons and

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.