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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:36:02+00:00 2026-06-10T02:36:02+00:00

I have a UIView subclass with the drawRect: method overriden. In there, I have

  • 0

I have a UIView subclass with the drawRect: method overriden. In there, I have long lines of generated code that draw something.

The generated code has a problem of having all the vertices/coordinates of the paths/lines hard-coded. So, to draw a 100×100 square, it would start at 0,0 and go to 0,100 -> 100,100 -> 100,0. To make this shape scale based on the UIView bounds property was done as follows:

// This is the size of the drawing. I always know this value beforehand.
CGSize contentSize = SHAPE_XMARK_SIZE;
CGFloat scaleX = self.bounds.size.width / contentSize.width;
CGFloat scaleY = self.bounds.size.height / contentSize.height;

CGContextScaleCTM(context, MIN(scaleX, scaleY), MIN(scaleX, scaleY));

All good as far as the scale is concerned. Now, I would like the position of the drawing to be ralative to the bounds, too. I want to somehow make the drawRect: method align the drawing based on the bounds, too.

I am thinking of something like:

  1. push context method
  2. draw code
  3. pop context
  4. position previous context

Is that a sane approach? Or is the push and pop context not able to accomplish such sorcery?


One Simple Approach:

To answer the “what have you tried” question, I tried making the view that I draw isolated with a bounds size equal to the drawing size, then embed that view inside another view… and it works. However, this method is tedious and I would prefer avoiding it, if possible.

  • 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-10T02:36:04+00:00Added an answer on June 10, 2026 at 2:36 am

    Pfft, the answer was super simple. Me 1, SO 0, I guess.

    CGContextTranslateCTM(context, xOffset, yOffset);
    

    Yeah, I somehow overlooked the fact that an awesome function should exist to translate CGContextRef, since the scale function existed.

    The rest is all simple math to achieve vertical & horizontal alignment.

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

Sidebar

Related Questions

I have a subclass of UIView that implements code to draw CoreText. In the
I have a UIView subclass that has the following in drawRect: for(int j=0; j<[myArray
I have a UIView subclass that draws itself when -drawRect: is called. It only
I have a UIView subclass ( CustomView for purposes of this question) that has
I want to have a UIView subclass that has a border image, but I
I have a subclass of UIView in which it has a drawRect in it.
I have a UIView Subclass that has a frame/image with buttons in it. I'm
I have a UIView subclass that draws a simple rectangle with this code: -
I have created a UIView subclass in order to implement a custom drawRect method.
So I have a Subclass of UIView that is suppose to detect touches. The

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.