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

  • Home
  • SEARCH
  • 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 6757735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:41:24+00:00 2026-05-26T13:41:24+00:00

I have a custom UIView that draws an image in the draw rect method.

  • 0

I have a custom UIView that draws an image in the draw rect method. My problem is UIView wants me to initWithFrame when I allocate the object. The frame of the view needs to be the same as the image size and width. The problem is that I don’t get that image size and width until the drawRect method of the view I am creating. Inside the drawRect view changing the UIView’s frame to the image dimensions seems to have no effect. So what I want to do is something like this:

MyCustomView *myCustomView = [[MyCustomView alloc] initWithFrame: CGrectMake(0,0,0,0)];

Then inside MyCustomView’s drawRect method I want to somehow change the frame of the view to be the frame size of the image I am creating.

How do I get my desired outcome? An no, creating the image before I init my custom view and putting the size dimensions into the initWithFrame is not an option.

This is my current code in the view’s drawRect method

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imgName]];
CGSize imgSize = [image size];
CGRect newrect = CGRectMake(self.frame.origin.x, self.frame.origin.y, imgSize.width, imgSize.height);
self.frame = newrect; //This does nothing!!
CGRect imgRect = CGRectMake(0, 0, imgSize.width, imgSize.height);
[image drawInRect:imgRect];
  • 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-26T13:41:25+00:00Added an answer on May 26, 2026 at 1:41 pm

    If you look at the docs for setFrame it says:

    This method, in setting the frame rectangle, repositions and resizes
    the receiver within the coordinate system of its superview. It neither
    redisplays the receiver nor marks it as needing display. You must do
    this yourself with display or setNeedsDisplay:.

    It will not redisplay – typically after setting the frame you call setNeedsDisplay. but, this highlights your chicken and egg problem. setNeedsDisplay causes drawrect to fire.

    Also, as Sulthan pointed out, doing something heavy like loading images in drawRect is not a good idea. It’s heavy weight and things like drawRect get called repeatedly on resize etc…

    I would either create an init pattern (even though you said not an option), or create a property to set the image and have the image load at that time.

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

Sidebar

Related Questions

I have a custom UIView class. Inside that view, there is an image view.
I have a custom UIView that acts as a layout object. If I add
I have a custom UIView which is drawn using its -[drawRect:] method. The problem
I have a custom UIView that generates a set of subviews and display them
I have a custom UIView subclass that i'm trying to use as a header
I have a custom view that subclasses UIView. It has a touchesEnded event, however
In one of my apps, I have a custom drawn semitransparent UIView that I
I have a custom UIView which I named UIWheel that I am rotating upon
I have a UIScrollView to which I append multiple custom UIView's that contain UITextFields...
I have created a UIView subclass in order to implement a custom drawRect method.

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.