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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:50:19+00:00 2026-05-20T19:50:19+00:00

I’m trying to create programatically a custom view controller with subviews which center on

  • 0

I’m trying to create programatically a custom view controller with subviews which center on the screen. I’ve succeeded, and when I navigate to this view, the centered view appears in the center, and rotates correctly when you rotate the device.

But, in this navigation application, if I enter the custom view when not in portrait mode, then the view which is supposed to be centered does position itself in a place which is not the center of the screen. I’ve put all the necessary autoresize properties on the views, the controller, the parent, the grandmother, the holy virgin… and I’ve run out of things to stick the autoresize mask to, and still the example looks bad.

I’m sure I’m missing a call to some magical method which will fix everything, but I’ve not figured what to call or where (setNeedsDisplay? setNeedsLayout?). To showcase this problem I’ve created a full example available at https://github.com/gradha/iPhone-centered-rotation-test which you can clone and run in the simulator or device. I created this from Apple’s navigation controller, simply adding a fake cell which pushes the view I create manually.

The custom view can be found at https://github.com/gradha/iPhone-centered-rotation-test/blob/master/Classes/CenteredViewController.m and here is the loadView method in charge of creating the centered subviews:

/* Try to force the parent view to be as we want it. */
self.view.backgroundColor = [UIColor yellowColor];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight |
    UIViewAutoresizingFlexibleWidth;

/* Top left blue square, for reference. */
blue_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 130, 130)];
[self.view addSubview:blue_];
blue_.backgroundColor = [UIColor blueColor];
[blue_ release];

/* Create red centered rectangle. */
red_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
red_.backgroundColor = [UIColor redColor];
red_.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin;
red_.center = self.view.center;
[self.view addSubview:red_];
[red_ release];

Here’s a link to a screen capture walkthrough of the application, first entering in portrait mode and rotating the screen (works OK), then entering in landscape mode and rotating (depending on the rotation side it looks quite bad). When you enter the view in landscape mode, depending on the orientation the red square will have a top left corner of 196×34 or 140×34, which is too much of a difference.

What am I missing to make these subviews center properly when entering the view in landscape mode and follow auto rotation?

  • 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-20T19:50:19+00:00Added an answer on May 20, 2026 at 7:50 pm

    It appears that in one of the landscape orientations, in loadView, the controller’s view origin is set to (0, 20) which is confusing the rotation and autoresizing that occurs after the view is added to the screen. Oddly enough, it’s (0, 0) in all other orientations.

    To fix the problem, add this line after [super loadView]:

    self.view.frame = self.view.bounds;
    

    To elaborate: since the frame origin is not at (0, 0), the concept of self.view.center is incorrect in the context it is being used. The center property is the center of that view as visible in its superview. What you really want is the center of the view’s bounds.

    By resetting the view frame to the view’s bounds, you effectively change the origin back to (0, 0), and center can be used properly.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.