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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:35:07+00:00 2026-05-27T06:35:07+00:00

I am trying to draw a rectangle on my UIView subclass, and everything is

  • 0

I am trying to draw a rectangle on my UIView subclass, and everything is working except for the width of the line is doubled (I’m using the iPhone Simulator atm, on the Retina version).

This is the drawRect method:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect innerRect = CGRectInset(rect, 5, 5);
    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextSetLineWidth(context, 2);
    CGContextMoveToPoint(context, CGRectGetMinX(innerRect), CGRectGetMinY(innerRect));
    CGContextAddLineToPoint(context, CGRectGetMaxX(innerRect), CGRectGetMinY(innerRect));
    CGContextAddLineToPoint(context, CGRectGetMaxX(innerRect), CGRectGetMaxY(innerRect));
    CGContextAddLineToPoint(context, CGRectGetMinX(innerRect), CGRectGetMaxY(innerRect));
    CGContextClosePath(context);
    CGContextStrokePath(context);
}

This code draws a rectangle in the correct place but with width 4px, rather than the specified 2px.

Looking on the internet, I see I may have to set the scale factor, so I tried adding :

    self.contentScaleFactor = [UIScreen mainScreen].scale;
    self.layer.contentsScale = [UIScreen mainScreen].scale;

at the beginning of the method, and when that didn’t work I tried:

    self.contentScaleFactor = 2;
    self.layer.contentsScale = 2;

(as 2 is the number that should be returned by the scale method for retina displays)
And that didn’t work either.

Is this just a problem with the simulator, that will be fixed when I run it on device? (I upgraded it to 5.1, but forget to update Xcode, so I’m currently 2 hours into the 7 hour wait for the Xcode update to finish downloading)

Or is there something I’m missing?

  • 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-27T06:35:08+00:00Added an answer on May 27, 2026 at 6:35 am

    This is correct behavior.

    You are specifying a width of 2 points (not pixels). On a standard screen that would translate to 2 pixels, but on a retina display that translates to 4 pixels. You should also notice that the size of the rect you are passed does not change with standard or retina either. Apple encourages drawing be done with points not pixels.

    A good reference is the “WWDC 2011 Session 129 – Practical Drawing for iOS Developers” video. While the video as a whole is informative, the meat of what your talking about starts at about 10:24 in to the video.

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

Sidebar

Related Questions

I've got a uiview subclass where I'm trying to draw a rounded rectangle with
I am simply trying to draw a rectangle inside of a panel using flex4.
I'm trying to draw a rectangle on my screen using the win32 python libs.
I am trying to draw a rectangle shape in XNA using spritebatch. I have
I am trying to draw an image into a UIView subclass overriding drawRect. This
I'm trying to draw a rounded rectangle with a border around it using a
I am trying to just draw a Rectangle on a PictureBox that is on
I am trying to draw a series of rectangles using OpenGL but some of
I'm trying to draw a polygon using c# and directx All I get is
I'm trying to draw some simples lines with the iPhone/Touch SDK. I'd like to

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.