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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:30:33+00:00 2026-05-23T16:30:33+00:00

Hi i am making a sample app in which i wanto create a square

  • 0

Hi i am making a sample app in which i wanto create a square for which i used the following code

- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawRect:CGRectMake(0, 0, 300, 200)];
    [[self view] setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect
{
    NSLog(@"drawRect");

    CGFloat centerx = rect.size.width/2;
    CGFloat centery = rect.size.height/2;
    CGFloat half = 100/2;
    CGRect theRect = CGRectMake(-half, -half, 100, 100);

    // Grab the drawing context
    CGContextRef context = UIGraphicsGetCurrentContext();

    // like Processing pushMatrix
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, centerx, centery);

    // Uncomment to see the rotated square
    //CGContextRotateCTM(context, rotation);

    // Set red stroke
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    {
        CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
    }
    // Draw a rect with a red stroke
    CGContextFillRect(context, theRect);
    CGContextStrokeRect(context, theRect);

    // like Processing popMatrix
    CGContextRestoreGState(context);
    [[self view] setNeedsDisplay];

}

But nothing is drawn on screen , dont know wheres the issue is .When i debug it the CGContextRef context was always 0x0 , i dont know why its 0x0 always am i missing something in my code.

  • 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-23T16:30:33+00:00Added an answer on May 23, 2026 at 4:30 pm

    It looks like you’re trying to draw in a subclass of UIViewController. You need to subclass UIView to override the drawRect: method, which is then called automatically with a valid graphics context in place. You almost never call this method yourself.

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

Sidebar

Related Questions

I’m making a simple LOB app which loads data from an XML file and
I am making a simple app in which filled circles bounce around the screen.
I am making a simple web-app which requires login for the admin page. I
I am making an application which implements the following structure : MainWindow |_ tabBar
I am making an app which required my checkboxes to be checked at once
I'm making something like an augmented reality app, in which I have an OpenGL
I'm making a media playback app which gets given uncompressed linear PCM (über raw)
I am making an simple Blackery Berry app (widget) which uses simple HTML/JS to
I am making a simple mac app in which i want to switch windows.
I am making an app in which i am using view based application. My

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.