views:

653

answers:

2

Hello,

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.

+2  A: 

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];


 }   
}
Per Brown
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): http://stackoverflow.com/questions/2659400/automatically-sizing-uiview-after-adding-to-window
Kristopher Johnson
A: 

Have the exact same problem, so, don't think you were doing anything wrong. Thanks for this!

Mike