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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:51:33+00:00 2026-06-08T07:51:33+00:00

Solution: For those who view this someday in the future, the solution I used

  • 0

Solution: For those who view this someday in the future, the solution I used was indeed viewDidLayoutSubviews. The solution was actually rather complex – I had to calculate several scaling values and dynamically re-size the Art view every time the page needed a re-layout. There were several odd problems to handle, but after each was taken down the implementation feels pretty solid.

If anybody runs in to a similar problem later on, let me know and I can post the relevant code.


I’ve got a ‘blank’ UIView, a subview that is an UIImageView containing a single image, and a second subview that is basically a collection of CGContext arcs and lines and all that.

Where I’m at: I’ve placed the UIImageView subview on top of the UIView so that its image is the ‘background’. Then I placed the CGContext arcs and lines subview on top of that (I’ll call this the Art subview for clarity). All good. Everything displays perfectly and aligned.

Problem: When I rotate, things get screwy. The arcs and lines on the Art subview end up at the wrong coordinates, and depending on my autoresizingmask settings the image gets stretched, etc. I can fix one of these problems at a time, but I can’t find the right combination to fix them both!

Things I’ve Tried: I’ve tried just about every autoresizingMask option & combination of options, but per above I can’t quite lick the problem with those. Given that I also tried using some custom code in viewDidLayoutSubviews, but this felt really flimsy and much less extensible vs. using autoresizingMasks. So I abandoned the path after making some nominal progress.

What I’ve Learned: As long as I bind my UIImageView frame and the Art subview frame to the same dimensions, then the arcs and lines stay at the proper coordinates. That is probably the easiest way to state my goal: to have a UIImageView that stays in the correct aspect ratio (not just the image within, but the view itself), and then match the Art subview exactly to its frame – even as the screen rotates, etc.

Here is a diagram of what I’d like to achieve:

+ = UIView

~ = UIImageView subview

. = Art subview

Portrait

Wherein the image within the UIImageView takes up basically the whole screen (though not quite), and the Art subview is layered on top of it with the dots below representing a crude line/arc.

++++++++++
+~~~~~~~~+
+~   .  ~+
+~  .   ~+
+~ .    ~+
+~ .    ~+
+~~~~~~~~+
++++++++++

Landscape

Wherein the UIImageView sublayer maintains its aspect ratio, and the Art sublayer stays ‘locked’ to the image within the UIImageView.

++++++++++++++++++++++++++
+         ~~~~~~~~       +
+         ~   .  ~       +
+         ~  .   ~       +
+         ~ .    ~       +
+         ~ .    ~       +
+         ~~~~~~~~       +
++++++++++++++++++++++++++

My View Controller (where I think the problem is – also removed all autoresizingMask settings to clean up the code)

- (void)viewWillAppear:(BOOL)animated {
    // get main screen bounds & adjust to include apple status bar
    CGRect frame = [[UIScreen mainScreen] applicationFrame];

    // get image - this code would be meaningless to show, but suffice to say it works
    UIImage *image = [.. custom method to get image from my image store ..];

    // custom method to resize image to screen; see method below
    image = [self resizeImageForScreen:image];

    // create imageView and give it basic setup
    imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setUserInteractionEnabled:YES];

    [imageView setFrame:CGRectMake(0,
                                   0,
                                   image.size.width, 
                                   image.size.height)];

    [imageView setCenter:CGPointMake(frame.size.width / 2,
                                     frame.size.height / 2)];

    [[self view] addSubview:imageView];

    // now put the Art subview on top of it
    // (customArtView is a subclass of UIView where I handle the drawing code)
    artView = [[customArtView alloc] initWithFrame:imageView.frame];

    [[self view] addSubview:artView];
}

the resizeImageForScreen: method (this seems to be working fine, but I figured I’d include it anyway)

- (UIImage *)resizeImageForScreen:(UIImage *)img {

    // get main screen bounds & adjust to include apple status bar
    CGRect frame = [[UIScreen mainScreen] applicationFrame];

    // get image
    UIImage *image = img;

    // resize image if needed
    if (image.size.width > frame.size.width || image.size.height > frame.size.height) {

        // Figure out a scaling ratio to make sure we maintain the same aspect ratio
        float ratio = MIN(frame.size.width / image.size.width, frame.size.height / image.size.height);

        CGRect newImageRect;
        newImageRect.size.width = ratio * image.size.width;
        newImageRect.size.height = ratio * image.size.height;
        newImageRect.origin.x = 0;
        newImageRect.origin.y = 0;

        // Create a transparent context @ the newImageRect size & no scaling
        UIGraphicsBeginImageContextWithOptions(newImageRect.size, NO, 0.0);

        // Draw the image within it (drawInRect will scale the image for us)
        [image drawInRect:newImageRect];

        // for now just re-assigning i since i don't need the big one
        image = UIGraphicsGetImageFromCurrentImageContext();

        // Cleanup image context resources; we're done!
        UIGraphicsEndImageContext();

    }

    return image;
}
  • 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-08T07:51:35+00:00Added an answer on June 8, 2026 at 7:51 am

    There is no combination of autoresizing flags and content modes that will do what you want. Using viewDidLayoutSubviews is one reasonable way to handle the layout. That’s why it exists: the autoresizing flags are pretty limited.

    A different approach is to change your -[ArtView drawRect:] method so that the autoresizing flags can do what you want. You can make your drawRect: method implement the same algorithm that UIImageView implements for UIViewContentModeScaleAspectFit.

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

Sidebar

Related Questions

My current solution will suck sometimes EDIT For those who don't understand,see this example:
editing this page for those who want to read this in the future :P
As those who have worked on such a project you probably know this site:
For those who still don't know about Responsive Design I suggest this link As
this question is only for those who use the MDI from Codeplex . I
I've implemented a simple PowerShell NavigationCmdletProvider . For those who don't know, this means
How many projects in a single solution is acceptable? And for those apps that
Solution at end of this post. By default the time is set to one
I have a controller view who is using thoses 2 functions: [appDelegate getFichesInfo:[[self.fichesCategory idModuleFiche]
I'm writing a contacts & events database and want to view those contacts that

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.