Subview Doesnt AutoSize When Added to Root View Controller

旧巷老猫 提交于 2020-01-23 03:32:05

问题


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.


回答1:


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


 }   
}


来源:https://stackoverflow.com/questions/2592447/subview-doesnt-autosize-when-added-to-root-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!