Subview Doesnt AutoSize When Added to Root View Controller
Asked Answered
L

1

1

I have a root view controller that will have up to 10 or so subviews. I am implementing autorotation/autosize accross the entire app.

My problem is this: - When I allocate all the view controllers and add each as a subview to the root controller during startup, everything works as it should. The only problem is that each view controller needs time to initialize. This causes my application to load very slowly.

  • Instead I am trying to allocate the view controllers as they are required. Now I find that if the application goes into Landscape, and I allocate a view controller that is designed in portrait, it will autorotate but the autosize doesnt happen.

  • In other words as soon as the subview is added to the root controller in portrait mode it rotates and sizes correctly (and stays that way). If the subview is added when the root controller is in landscape it rotates but doesnt autosize (and view sizes remain messed up rotating back to portrait)

  • I have tried to force an autosize by calling SetNeedsLayout, SetNeedsDisplay, and LayoutIfNeeded but nothing works. I know i could probably do this manually by determining the root controllers orientation and resizing the subviews appropriately, but this is a lot of work for something that should work automatically.

  • Am I missing something? Any help would be appreciated. My project is an iPad port from an iPhone app, the iPhone app doesnt rotate so Im not sure if this may be something wrong with the 3.2 beta.

Lycopodium answered 7/4, 2010 at 12:40 Comment(0)
P
2

After wrestling with this for a while I added the following code to my subview. It executes after the view is added to the root view controller. So far it seems to work.

-

(void) AdjustFrame{
 UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
 if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight))
 {
  CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
  CGRect newFrame =  CGRectMake(0.0, 0.0, applicationFrame.size.height, applicationFrame.size.width);
  [self.view setFrame:newFrame];
  [self.view setNeedsDisplay];


 }   
}
Pindaric answered 9/4, 2010 at 21:30 Comment(1)
I wish I'd found this a couple of days ago, when I had a similar issue. Here is my question/answer (very similar to yours): #2659900Exogamy

© 2022 - 2024 — McMap. All rights reserved.